ofxAvCodec - ffmpeg wrapper for reading/writing audio files


#21

yep, use it almost every day actually.

i take it this is a linker error?

go through your project settings, remove the DLLs, and link to the lib files instead.


#22

Yes, it is a linker error.

The Project Generator is adding those dlls to the link, so by default the additional dependencies looks like this:

I have removed the links to the dlls as you can see below, you can also see I’ve added the libs into the addons folder

After doing that when I run I get the following error:

What have I missed??

Thanks


#23

that means it works, all that’s missing is to copy the dlls to the bin folder alongside your exe.
either do it manually, or use robocopy/xcopy in your post build step.


#24

@kritzikratzi it is working now, thanks for the help and for the great addon!


#25

Hello @kritzikratzi, nice addon! Could you give me a hint on how to use it to write an ofSoundStream to a file? I’m trying to record the microphone stream to a .wav file. Specifically, how to use the write function from the ofxAvAudioWriter class? I’ve read the instructions in your Github but I still don’t get it… many thanks!


#26

hi!

you might want to use something simpler to write wav files.
if you google “wavfile writer c++” you should find lots of single file solutions. (for instance this: https://github.com/HellicarAndLewis/Hub/blob/master/addons/AudioEngine/core/WavFile.h )

you’d do it roughly like this: (also with any wavwriter)

// this goes into ofApp.h
ofSoundStream soundStream; 
ofxAvAudioWriter writer;
bool recording = false; 

// in your setup: 
soundStream.setup(this, 0, 1, 44100, 512, 4 ); 

// this goes also in setup(), or whenever you want to start recording
writer.setup(soundStream.getSampleRate(), soundStream.getNumInputChannels); // same samplerate, same number of channels!
writer.addMeta("title", "my cool song"); 
if( writer.open(ofToDataPath("testo.wav"), "wav" ) ) recording = true; 
// in the open() command you have the option to provide a different sample rate/channel count for the file 
// than you are using to feed the data. 
// but sticking with the input format should be the easiest 

// this goes in your ofAp::() audioOut(float * buffer, int bufferSize, int nChannels)
// of course, only call this when the writer is open
if( recording ) writer.write(buffer, N);

// this goes in ofApp::exit(), or whenever you want to stop recording
recording = false; // might wanna wait a bit here to make sure no one is writing anymore
writer.close();

#27

Hey @kritzikratzi, many thanks for your reply. Following your suggestions and the instructions in your Github, here´s what I’ve implemented so far:

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	soundStream.setDeviceID(2);
	soundStream.setup(this, 0, 2, 48000, 512, 4);

	isRecording = false;
}

//--------------------------------------------------------------
void ofApp::update(){
}

//--------------------------------------------------------------
void ofApp::draw(){
	string infoText =
		"FPS: " + ofToString(ofGetFrameRate());
	ofDrawBitmapStringHighlight(infoText, 50, 50);
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){
	if (key == 'r' && !isRecording) {
		audioRecorder.setup(48000, 2);
		audioRecorder.open(ofToDataPath(ofGetTimestampString()) + ".wav", "wav");
		isRecording = true;
	}

	if (key == 'r' && isRecording) {
		//Stop recording
		audioRecorder.close();
		isRecording = false;
	}
}

//--------------------------------------------------------------
void ofApp::audioIn( float *input, int bufferSize, int nChannels ){
	if (isRecording) {
		int N = 128;
		float * buffer = new float[N];
		int num = 0;

		while (num < 3000) {
			for (int i = 0; i < N; i++) {
				buffer[i] = sinf(num*TWO_PI * 400 / 48000.0);
				num++;
			}
			audioRecorder.write(buffer, N);
		}
	}
}

The problem is that I’m getting this error when I open the writer:

[pcm_s16le @ 0000000009dd56c0] Specified sample format 48000 is invalid or not supported
could not open codec

Why 48000 would not be supported? I’m not sure if it’s anything related, but I’m calling the write function in audioIn(), since I don’t need to output the stream to the speakers.

Thanks again!


#28

Well, I ended up investigating your suggestion of using a simpler class and got fairly good results following this discussion:

Thanks again for your attention :wink: