80 lines
1.7 KiB
C
80 lines
1.7 KiB
C
#include "audio.h"
|
|
#include <portaudio.h>
|
|
|
|
|
|
#define AUDIO_FREQUENCY 96000
|
|
static PaStream *stream;
|
|
static size_t buffer_position = 0;
|
|
static GB_sample_t buffer[8192];
|
|
|
|
|
|
bool GB_audio_is_playing(void)
|
|
{
|
|
return stream && Pa_IsStreamActive(stream);
|
|
}
|
|
|
|
void GB_audio_set_paused(bool paused)
|
|
{
|
|
if (!stream) return;
|
|
|
|
if (paused) {
|
|
Pa_StopStream(stream);
|
|
}
|
|
else {
|
|
Pa_StartStream(stream);
|
|
}
|
|
}
|
|
|
|
void GB_audio_clear_queue(void)
|
|
{
|
|
buffer_position = 0;
|
|
}
|
|
|
|
unsigned GB_audio_get_frequency(void)
|
|
{
|
|
return stream? Pa_GetStreamInfo(stream)->sampleRate : 0;
|
|
}
|
|
|
|
size_t GB_audio_get_queue_length(void)
|
|
{
|
|
return buffer_position;
|
|
}
|
|
|
|
#include <stdio.h>
|
|
void GB_audio_queue_sample(GB_sample_t *sample)
|
|
{
|
|
if (!stream) return;
|
|
|
|
if (buffer_position < sizeof(buffer) / sizeof(buffer[0])) {
|
|
buffer[buffer_position++] = *sample;
|
|
}
|
|
else {
|
|
printf("Overflow\n");
|
|
}
|
|
|
|
size_t write_legnth = Pa_GetStreamWriteAvailable(stream);
|
|
if (write_legnth > buffer_position) {
|
|
return;
|
|
}
|
|
if (write_legnth) {
|
|
printf("Pushing %zu out of %zu (Distance: %zu)\n", write_legnth, buffer_position, buffer_position - write_legnth);
|
|
Pa_WriteStream(stream, buffer, write_legnth);
|
|
memmove(buffer, buffer + write_legnth, sizeof(buffer[0]) * (buffer_position - write_legnth));
|
|
buffer_position -= write_legnth;
|
|
}
|
|
}
|
|
|
|
void GB_audio_init(void)
|
|
{
|
|
Pa_Initialize();
|
|
Pa_OpenDefaultStream(&stream,
|
|
0,
|
|
2,
|
|
paInt16,
|
|
AUDIO_FREQUENCY,
|
|
paFramesPerBufferUnspecified,
|
|
NULL,
|
|
NULL);
|
|
|
|
}
|