-
I have a basic (awesome) multitrack recorder going where I can record from device input many
ma_uint32 framesChunk = 512;
ma_uint32 startFrame = 0;
ma_uint32 endFrame = framesChunk;
// some buffer to put the output of the underlying `ma_node_graph` data in
// (allocated and zeroed)
Buffer buffer;
while(endFrame < bufferSize)
{
// read buffer at start and end frames and return a pointer to where to access the buffer
// next (pPCMFrames)
pPCMFrames = readBuffer(&buffer, startFrame, endFrame);
// populate the buffer with underlying node_graph data via engine
ma_engine_read_pcm_frames(&g_engine, pPCMFrames, framesChunk);
// encode and write the buffer, which should have data to disk
ma_encoder_write_pcm_frames(&encoder, pPCMFrames, framesChunk);
// increment the end frame index by framesChunk
endFrame += framesChunk;
// increment the start frame index by framesChunk
startFrame + = framesChunk
}
ma_encoder_uninit(&encoder); Can something like option 2 work? I'm thinking that if it made sense, it would loop much faster than the callback and be much quicker to encode. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I've been working on implementing this and it's working to a degree. I mean I can hear the sounds and whatnot, but it's also loaded with static. I wonder if it has to do with the buffer not being setup properly. I'm going to continue to dig in and post some code, if I can't figure it out. |
Beta Was this translation helpful? Give feedback.
-
I got it to work! I had the wrong format on the second
void writeWav(){
// logic to set the seek_to position and start & and times for all sounds to their recorded positions
// ...
// todo: init the encoder before this function is called.
ma_encoder encoder2;
char * outputFile = "/Users/me/Documents/outall.wav";
ma_encoder_config encoderConfig2 = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 2, 44100);
if (ma_encoder_init_file(outputFile, &encoderConfig2, &encoder2) != MA_SUCCESS) {
printf("Failed to initialize output file.\n");
}
// stop and reset engine
ma_engine_set_time(&g_engine, 0);
ma_engine_stop(&g_engine);
ma_uint32 framesChunk = 512;
ma_uint32 endFrame = framesChunk;
// setup a buffer
ma_audio_buffer_ref * pBufferRef3;
pBufferRef3 = (ma_audio_buffer_ref *)malloc(sizeof(*pBufferRef3));
// todo: ma_audio_buffer_ref_get_length_in_pcm_frames or something
while(endFrame < 400000){
// populate the buffer with underlying node_graph data via engine
ma_engine_read_pcm_frames(&g_engine, pBufferRef3, framesChunk, NULL);
// encode and write the buffer, which should have data to disk
ma_encoder_write_pcm_frames(&encoder2, pBufferRef3, framesChunk, NULL);
// increment the end frame index by framesChunk
endFrame += framesChunk;
}
ma_encoder_uninit(&encoder2);
free(pBufferRef3);
}`` |
Beta Was this translation helpful? Give feedback.
-
I solved this by having a counter in the callback so that during the initial recording all frames are counted up, then when I am writing I have counter in the while loop so that it's: while(true){
//...read frames
if(framesWritten >= framesRecorded){
break;
}
} Not sure if having a counter in the callback would cause a hit to low latency performance though. |
Beta Was this translation helpful? Give feedback.
I got it to work! I had the wrong format on the second
ma_encoder
that I used to write it to disk! I might be learning something after all! This is how I did it below.Questions: