Skip to content

Instantly share code, notes, and snippets.

@bjorn-nesby
Created February 29, 2024 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjorn-nesby/31f241c063da032f86cbaf19c862bc1c to your computer and use it in GitHub Desktop.
Save bjorn-nesby/31f241c063da032f86cbaf19c862bc1c to your computer and use it in GitHub Desktop.
/*
Demonstrates an issue with certain aiff/wav files, where seeking can result in distorted output.
*/
#define MA_DEBUG_OUTPUT
#define MINIAUDIO_IMPLEMENTATION
#include <miniaudio.h>
#include <stdio.h>
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
ma_decoder* pDecoder = (ma_decoder*)pDevice->pUserData;
if (pDecoder == NULL) {
return;
}
ma_decoder_read_pcm_frames(pDecoder, pOutput, frameCount, NULL);
(void)pInput;
}
int main(int argc, char** argv)
{
ma_result result;
ma_decoder decoder;
ma_device_config deviceConfig;
ma_device device;
if (argc < 2) {
printf("No input file.\n");
return -1;
}
result = ma_decoder_init_file(argv[1], NULL, &decoder);
if (result != MA_SUCCESS) {
printf("Could not load file: %s\n", argv[1]);
return -2;
}
deviceConfig = ma_device_config_init(ma_device_type_playback);
deviceConfig.playback.format = decoder.outputFormat;
deviceConfig.playback.channels = decoder.outputChannels;
deviceConfig.sampleRate = decoder.outputSampleRate;
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = &decoder;
if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) {
printf("Failed to open playback device.\n");
ma_decoder_uninit(&decoder);
return -3;
}
if (ma_device_start(&device) != MA_SUCCESS) {
printf("Failed to start playback device.\n");
ma_device_uninit(&device);
ma_decoder_uninit(&decoder);
return -4;
}
// output a bit of statistics
printf("device.playback.channels: %i\n", device.playback.channels);
printf("input format: %d\n", decoder.outputFormat);
printf("input channels: %d\n", decoder.outputChannels);
printf("input sampleRate: %d\n", decoder.outputSampleRate);
ma_uint64 lengthInPCMFrames = 0;
result = ma_data_source_get_length_in_pcm_frames(&decoder, &lengthInPCMFrames);
printf("input number of frames: %llu \n", lengthInPCMFrames);
printf("Press Enter to seek (you might want to turn down your speakers first :-)");
getchar();
result = ma_data_source_seek_to_pcm_frame(&decoder, lengthInPCMFrames/2);
printf("Press Enter to quit...");
getchar();
ma_device_uninit(&device);
ma_decoder_uninit(&decoder);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment