pschatzmann / arduino-libflac

libflac codec library for Arduino
Other
7 stars 3 forks source link

Exiting with `FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR` on AIThinker AC101 #1

Open learex opened 1 year ago

learex commented 1 year ago

I have a small problem: Using the small MWE below, I cannot get the FLAC Encoder to work.

[E] CodecFLAC.h : 419 - write_callback 28 -> 0
[E] CodecFLAC.h : 339 - ERROR: initializing decoder: FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR
[E] CodecFLAC.h : 341 -  -> FLAC__STREAM_ENCODER_CLIENT_ERROR

Replacing new FLACEncoder() with new AACEncoderFDK() works perfectly fine though. (and changing "/rec.flac" to "/rec.aac")

I'm using an AIThinker AC101 V2.2 2748.

#include "AudioTools.h"
#include "AudioLibs/AudioKit.h"

#include "AudioCodecs/CodecFLAC.h"
#include "AudioCodecs/CodecAACFDK.h"

#include "AudioKitHAL.h"

#include "SPI.h"
#include "SD.h"

const char * FILE_PATH = "/rec.flac";
const audio_hal_iface_samples_t SAMPLE_RATE = AUDIO_HAL_48K_SAMPLES;
const audio_hal_iface_bits_t BIT_DEPTH = AUDIO_HAL_BIT_LENGTH_16BITS;
const audio_hal_adc_input_t INPUT_DEVICE = AUDIO_HAL_ADC_INPUT_LINE1;

AudioKit i2s;
AudioKitStream kit;
File file;
EncodedAudioStream encodedStream(&file, new FLACEncoder());

const int BUFFER_SIZE = 512;
uint8_t buffer[BUFFER_SIZE];

bool recording = false;

void copier() {
  size_t len = i2s.read(buffer, BUFFER_SIZE);
  encodedStream.write(buffer, len); 
}

void recordStart(bool pinStatus, int pin, void * ref) {
  file = SD.open(FILE_PATH, FILE_WRITE);
  recording = true;
}

void recordEnd(bool pinStatus, int pin, void * ref) {
  if (recording) {
    copier();
    file.close();
    recording = false;
  }
}

void setup() {
  Serial.begin(115200);
  while(!Serial);

  AudioLogger::instance().begin(Serial, AudioLogger::Info);

  SD.begin(i2s.pinSpiCs(), AUDIOKIT_SD_SPI);

  auto cfg = i2s.defaultConfig(AudioInput);
  cfg.sd_active = true;
  cfg.sample_rate = SAMPLE_RATE;
  cfg.bits_per_sample = BIT_DEPTH;
  cfg.adc_input = INPUT_DEVICE;
  cfg.buffer_size = BUFFER_SIZE;
  i2s.begin(cfg);
  i2s.setVolume(1.0);
  encodedStream.resize(BUFFER_SIZE);

  AudioBaseInfo encodeCfg;
  encodeCfg.bits_per_sample = i2s.config().bitsPerSample();
  encodeCfg.channels = 2;
  encodeCfg.sample_rate = i2s.config().sampleRate();
  encodedStream.begin(encodeCfg);

  kit.audioActions().add(PIN_KEY1, recordStart, recordEnd);
}

void loop() {
  if (recording){
    copier();
  }

  kit.processActions();
}
pschatzmann commented 1 year ago

Did you compare your implementation with the test case: https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/tests/codecs/test-codec-flac/test-codec-flac.ino

Maybe you just selected some values which are not supported...

learex commented 1 year ago

It seems to be the writing to a file, adding the following to the code referenced breaks the encoder setup:

[...]
#include "SPI.h"
#include "SD.h"
[...]
File file;
EncodedAudioStream encoder(&file, new FLACEncoder()); // encode and write

Do you maybe know why? As per the documentation EncodedAudioStream should support File as an output stream type.

pschatzmann commented 1 year ago

Hmm, that's very unlikely. Unrelated to your issue I suggest either to use the AudioKitStream class only. Or the AudioKit with the AudioActions to process the key events. Mixing AudioKitStream and AudioKit is very confusing!

learex commented 1 year ago

I'm back at it. Do you have a working example that does something with FLAC on the AC101 Dev board lying around? This way I could work backwards and see if that example also works on my board, where it's going wrong.

pschatzmann commented 1 year ago

I think there is a bug in the encoder. I did not find any test case...