46 lines
1.1 KiB
C
46 lines
1.1 KiB
C
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_audio.h>
|
|
#include <SDL2/SDL_error.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
|
|
#define PCM_BUF_SIZE (1024*2*2*2)
|
|
|
|
static Uint8 *s_audio_buf = NULL;
|
|
static Uint8 *s_audio_pos = NULL;
|
|
static Uint8 *s_audio_end = NULL;
|
|
|
|
void audo_pcm_callback(void *data, Uint8 *stream, int len) {
|
|
SDL_memset(stream, 0, len);
|
|
|
|
if (s_audio_pos >= s_audio_end) {
|
|
return;
|
|
}
|
|
|
|
int remain_buffer_len = s_audio_end - s_audio_pos;
|
|
len = (len < remain_buffer_len) ? len : remain_buffer_len;
|
|
|
|
SDL_MixAudio(stream, s_audio_pos, len, SDL_MIX_MAXVOLUME/8);
|
|
s_audio_pos += len;
|
|
}
|
|
|
|
#undef main
|
|
int main(int argc, char* argv[]) {
|
|
int ret = -1;
|
|
FILE *audio_fd = NULL;
|
|
SDL_AudioSpec spec;
|
|
const char *path = "44100_16bit_2ch.pcm";
|
|
size_t read_buffer_len = 0;
|
|
|
|
if (SDL_Init(SDL_INIT_AUDIO)) {
|
|
fprintf(stderr, "SDL_Init(SDL_INIT_AUDIO) failed: %s\n", SDL_GetError());
|
|
return ret;
|
|
}
|
|
|
|
audio_fd = fopen(path, "rb");
|
|
if (!audio_fd) {
|
|
fprintf(stderr, "fopen(path, \"rb\")\n");
|
|
goto fail;
|
|
}
|
|
}
|