Trouble Saving AudioSourceChannelInfo to WAV file

Hello,

I am trying to save 5 seconds of my raw audio input into a wave file. I followed this example

I am currently able to save data to my WAV file but when I try and listen to the file I just hear a click and then the wave file closes. I can see that my program is increasing the file size tho so I must be doing something wrong.

any help is greatly appreciated. I am trying to save 8 channels and have a sample rate of 192000hz

void getNextAudioBlock(const AudioSourceChannelInfo& bufferToFill) override
{
//create wav buffer
AudioBuffer WavBuffer;
WavBuffer.setSize(8, 2048);

	//load the audio buffer into our wav buffer 
	for (int sample = 0; sample < bufferToFill.numSamples; ++sample)
	{
		for (int c = 0; c < 1; c++) {
			WavBuffer.setSample(c, sample, bufferToFill.buffer->getSample(c, sample));
		}
	}

	//Get file
	File WavFile = File::getCurrentWorkingDirectory().getParentDirectory().getParentDirectory().getChildFile("Files").getChildFile("SoundBoardMicRecording_1.wav");

	//Create Wav Formater and writer
	WavAudioFormat WavFormat;
	std::unique_ptr<AudioFormatWriter> WavWriter;

	WavWriter.reset(WavFormat.createWriterFor(new FileOutputStream(WavFile), 
		currentSampleRate,
		8,
		24,
		{},
		0));
	
	//save Wav Data before buffer gets modified
	if (WavWriter != nullptr) {
		WavWriter->writeFromAudioSampleBuffer(WavBuffer, 0, WavBuffer.getNumSamples());
	}

}

You must not create the wav writer in the getNextAudioBlock callback, that way you are just recreating the writer over and over hundreds of times a second! The writer needs to be a member variable of your class. edit : And be created/initialized outside of the getNextAudioBlock method, for example in the constructor or the prepareToPlay method.

The additional WavBuffer isn’t necessary, you can use the wavwriter with the buffer from the AudioSourceChannelInfo.

You should also delete the destination file, otherwise your application just keeps appending to the end of file during subsequent runs and that isn’t going to work when using the same file name.

When i move

File WavFile = File::getCurrentWorkingDirectory().getParentDirectory().getParentDirectory().getChildFile("Files").getChildFile("SoundBoardMicRecording_1.wav");
	std::unique_ptr<AudioFormatWriter> WavWriter;
	WavAudioFormat WavFormat;

To be Public member variables of the class I get a Jassert error

  if (headerPosition != output->getPosition() && ! output->setPosition (headerPosition))
        {
            // if this fails, you've given it an output stream that can't seek! It needs to be
            // able to seek back to go back and write the header after the data has been written.
            jassertfalse;
            return;
        }

Did you also move the code that creates the writer out from the getNextAudioBlock method? It could be put into the constructor of the class or the prepareToPlay method.

Update: I Solved the problem. It turns out that the sample rate on my built-in speakers was set lower than 192Khz so the wav file could not be played and resulted in a clicking. You can change this setting in the sound parameters of your playback device on windows.