Writing section of audio buffer to file


#1

Hi all,

I’m excited to be revisiting the great audio api, especially now that audio input on Linux is working (thanks!)! I have a project where I need to select sections of an audio file and export them as separate samples. Hacking the buffer player sample I was able to able to get smaller buffer players from a larger one. However, I can not figure out how to write these to disk like I can with a buffer recorder node. I can’t see any method to get an existing buffer into a recording node and have had no luck with memcpy so far.

Any advice greatly appreciated, thanks!

        int numFrames = .5 * mBufferPlayerNode->getNumFrames();
        int frameOffset = 0;
        int otherFrameOffset = 0;
        int numChannels = mBufferPlayerNode->getNumChannels();
        
        audio::BufferRef buffer = make_shared< audio::Buffer >( numFrames, numChannels );
        buffer->copyOffset(*mBufferPlayerNode->getBuffer(), numFrames, frameOffset, otherFrameOffset);
        
        auto ctx = audio::Context::master();
        audio::BufferPlayerNodeRef player = ctx->makeNode( new audio::BufferPlayerNode( buffer ) );
        mWaveformPlot.load( player->getBuffer(), getWindowBounds() );
        mBufferPlayerNode = player;
        mBufferPlayerNode >> mGain >> ctx->getOutput();
        mBufferPlayerNode->setEnabled(true);
        
        audio::BufferRecorderNodeRef recorder = make_shared< audio::BufferRecorderNode >( numFrames );
        //not sure what to do next...
        memcpy(recorder->getInternalBuffer(), buffer->getData(), numFrames);//< doesn't work

        recorder->writeToFile(filepath);

#2

Hey there,

You don’t need an audio::BufferRecorderNode to write a buffer to disk, that guy just makes it easier to write real-time audio samples into a buffer in a thread-safe manner. Take a look at audio::TargetFile. There’s some test code for how to use it directly here.

I do hope to get to audio::BufferViews at some point, as this comes up quite often and it seems like the ideal way to go. Still, the extra copying hasn’t proven to be detrimental to performance yet, so it hasn’t been prioritized.

cheers,
Rich


#3

Thanks Rich, worked a treat!
I’m sorted now but BufferView does sound great. For a project like this I’d use it to help visualise waveforms too big to fit on screen and also for these multiple file exports from one long waveform. Will keep an ear out!
Cheers,
nay.