mirror of
https://github.com/mackron/miniaudio.git
synced 2026-04-21 15:56:58 +02:00
API CHANGE: Add device callbacks to mal_device_config.
Rationale: 1) It allows the callbacks to be set at initialization time which feels a bit more intuitive to me. 2) It avoids the need to call mal_device_set_send_callback(), etc. 3) It's a bit more consistent with the onLog callback. Previously, onLog would be passed to mal_device_init(), whereas onSend, etc were set with mal_device_set_send_callback(), etc. which feels needlessly inconsistent.
This commit is contained in:
@@ -53,14 +53,17 @@ int main()
|
||||
config.sampleRate = 48000;
|
||||
config.bufferSizeInFrames = 0; // Use default.
|
||||
config.periods = 0; // Use default.
|
||||
config.onRecvCallback = on_recv_frames;
|
||||
config.onSendCallback = on_send_frames;
|
||||
config.onStopCallback = NULL;
|
||||
config.onLogCallback = NULL;
|
||||
|
||||
printf("Recording...\n");
|
||||
mal_device captureDevice;
|
||||
if (mal_device_init(&captureDevice, mal_device_type_capture, NULL, &config, NULL, NULL)) {
|
||||
if (mal_device_init(&captureDevice, mal_device_type_capture, NULL, &config, NULL)) {
|
||||
printf("Failed to initialize capture device.\n");
|
||||
return -2;
|
||||
}
|
||||
mal_device_set_recv_callback(&captureDevice, on_recv_frames);
|
||||
mal_device_start(&captureDevice);
|
||||
|
||||
printf("Press Enter to stop recording...\n");
|
||||
@@ -71,11 +74,10 @@ int main()
|
||||
|
||||
printf("Playing...\n");
|
||||
mal_device playbackDevice;
|
||||
if (mal_device_init(&playbackDevice, mal_device_type_playback, NULL, &config, NULL, NULL)) {
|
||||
if (mal_device_init(&playbackDevice, mal_device_type_playback, NULL, &config, NULL)) {
|
||||
printf("Failed to initialize playback device.\n");
|
||||
return -3;
|
||||
}
|
||||
mal_device_set_send_callback(&playbackDevice, on_send_frames);
|
||||
mal_device_start(&playbackDevice);
|
||||
|
||||
printf("Press Enter to quit...\n");
|
||||
|
||||
@@ -37,21 +37,17 @@ int main(int argc, char** argv)
|
||||
config.sampleRate = wav.sampleRate;
|
||||
config.bufferSizeInFrames = 0; // Use default.
|
||||
config.periods = 0; // Use default.
|
||||
config.onRecvCallback = NULL; // Not used for playback.
|
||||
config.onSendCallback = on_send_frames_to_device;
|
||||
config.onStopCallback = NULL;
|
||||
config.onLogCallback = NULL;
|
||||
|
||||
mal_device device;
|
||||
if (mal_device_init(&device, mal_device_type_playback, NULL, &config, NULL, NULL) != MAL_SUCCESS) {
|
||||
if (mal_device_init(&device, mal_device_type_playback, NULL, &config, &wav) != MAL_SUCCESS) {
|
||||
printf("Failed to open playback device.");
|
||||
drwav_uninit(&wav);
|
||||
return -3;
|
||||
}
|
||||
|
||||
// The pUserData member of mal_device is reserved for you.
|
||||
device.pUserData = &wav;
|
||||
|
||||
// This is the callback for sending data to a playback device when it needs more. Make sure
|
||||
// it's set before playing the device otherwise you'll end up with silence for the first
|
||||
// bunch of frames.
|
||||
mal_device_set_send_callback(&device, on_send_frames_to_device);
|
||||
mal_device_start(&device);
|
||||
|
||||
printf("Press Enter to quit...");
|
||||
|
||||
Reference in New Issue
Block a user