Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
utilize lambda expression for callback
Browse files Browse the repository at this point in the history
  • Loading branch information
MGTheTrain committed Apr 18, 2024
1 parent c89e9da commit 4669976
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 67 deletions.
26 changes: 0 additions & 26 deletions modules/audio/include/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,6 @@ bool startPlayback(AudioData *audioData);
* to close.
*/
void closeAudioFile(AudioData *audioData);

/**
* @brief Callback function for audio playback.
*
* This function is called by PortAudio when it needs more audio data to play.
* It reads audio data from the file into the output buffer.
*
* @param input Pointer to the input buffer (not used in playback).
* @param output Pointer to the output buffer where audio data should be
* written.
* @param frameCount Number of frames requested for playback.
* @param timeInfo Pointer to timing information for the callback (not used in
* this function).
* @param statusFlags Flags indicating possible errors or other information from
* PortAudio.
* @param userData Pointer to user data provided when the stream was opened.
* In this function, it should be a pointer to the AudioData
* struct.
*
* @return paContinue if playback should continue, paComplete if playback is
* complete.
*/
static int playbackCallback(const void *input, void *output,
unsigned long frameCount,
const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags, void *userData);
#ifdef __cplusplus
}
#endif
61 changes: 20 additions & 41 deletions modules/audio/src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ bool startPlayback(AudioData *audioData) {
Pa_GetDeviceInfo(outputParameters.device)->defaultHighOutputLatency;
outputParameters.hostApiSpecificStreamInfo = nullptr;

auto playbackCallback = [](const void *input, void *output, unsigned long frameCount,
const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags, void *userData) -> int {
float *out;
AudioData *audioData = (AudioData *)userData;
sf_count_t num_read;

out = (float *)output;
memset(out, 0, sizeof(float) * frameCount * audioData->info.channels);

num_read = sf_read_float(audioData->file, out,
frameCount * audioData->info.channels);

if (num_read < frameCount) {
return paComplete;
}
return paContinue;
};


err = Pa_OpenStream(&audioData->stream,
nullptr, // No input
&outputParameters, audioData->info.samplerate,
Expand Down Expand Up @@ -106,45 +126,4 @@ void closeAudioFile(AudioData *audioData) {
Pa_CloseStream(audioData->stream);
Pa_Terminate();
sf_close(audioData->file);
}

/**
* @brief Callback function for audio playback.
*
* This function is called by PortAudio when it needs more audio data to play.
* It reads audio data from the file into the output buffer.
*
* @param input Pointer to the input buffer (not used in playback).
* @param output Pointer to the output buffer where audio data should be
* written.
* @param frameCount Number of frames requested for playback.
* @param timeInfo Pointer to timing information for the callback (not used in
* this function).
* @param statusFlags Flags indicating possible errors or other information from
* PortAudio.
* @param userData Pointer to user data provided when the stream was opened.
* In this function, it should be a pointer to the AudioData
* struct.
*
* @return paContinue if playback should continue, paComplete if playback is
* complete.
*/
int playbackCallback(const void *input, void *output, unsigned long frameCount,
const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags, void *userData) {
float *out;
AudioData *audioData = (AudioData *)userData;
sf_count_t num_read;

out = (float *)output;
memset(out, 0, sizeof(float) * frameCount * audioData->info.channels);

num_read = sf_read_float(audioData->file, out,
frameCount * audioData->info.channels);

if (num_read < frameCount) {
return paComplete;
}

return paContinue;
}

0 comments on commit 4669976

Please sign in to comment.