Last active
April 14, 2018 19:41
-
-
Save woodrowbarlow/ada2fdbb75e9b6c907594df60cdf79e3 to your computer and use it in GitHub Desktop.
Decode a .wav file into signed 32-bit PCM samples with Dr Wav, then write those samples to the audio server via Pulse Audio.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compile with `gcc wav-pulse-example.c -lpulse-simple -lpulse -o wav-pulse-example` | |
// run with `./wav-pulse-example <path_to_wav_file>` | |
#include <stdio.h> | |
#include <stdlib.h> | |
// install dev libs from package "pulseaudio-libs-devel" (rpm) or "libpulse-dev" (deb) | |
#include <pulse/simple.h> | |
#include <pulse/error.h> | |
// Download dr_wav.h from: https://mackron.github.io/dr_wav | |
// put it in the same directory as this file | |
#define DR_WAV_IMPLEMENTATION | |
#include "dr_wav.h" | |
int main(int argc, char *argv[]) | |
{ | |
drwav* wav = drwav_open_file(argv[1]); | |
if (wav == NULL) { | |
return -1; | |
} | |
size_t sampleDataSize = (size_t) wav->totalSampleCount * sizeof(int32_t); | |
int32_t *sampleData = (int32_t *) malloc(sampleDataSize); | |
drwav_read_s32(wav, wav->totalSampleCount, sampleData); | |
drwav_close(wav); | |
pa_simple *pulse; | |
pa_sample_spec pulseSpec = { | |
.format = PA_SAMPLE_S16LE, | |
.channels = 2, | |
.rate = 44100, | |
}; | |
pulse = pa_simple_new(NULL, "TestApp", PA_STREAM_PLAYBACK, NULL, | |
"Music", &pulseSpec, NULL, NULL, NULL); | |
int error; | |
if (pa_simple_write(pulse, sampleData, sampleDataSize, &error) < 0) { | |
fprintf(stderr, "pa_simple_write() failed: %s\n", pa_strerror(error)); | |
} | |
pa_simple_free(pulse); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment