sine example working with dummy

This commit is contained in:
Andrew Kelley 2015-07-04 03:55:00 -07:00
parent 60b731ab4f
commit 664c50752a
3 changed files with 51 additions and 15 deletions

View file

@ -81,7 +81,7 @@ make
## Roadmap
0. sine example working with dummy backend linux, osx, windows
0. sine example working with dummy backend osx, windows
0. sine example working with pulseaudio backend linux
0. pipe record to playback example working with dummy linux, osx, windows
0. pipe record to playback example working with pulseaudio linux

View file

@ -10,6 +10,8 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
__attribute__ ((cold))
__attribute__ ((noreturn))
@ -23,8 +25,42 @@ static void panic(const char *format, ...) {
abort();
}
static void write_callback(struct SoundIoOutputDevice *device, int frame_count) {
fprintf(stderr, "write_callback\n");
static const float PI = 3.1415926535f;
static float seconds_offset = 0.0f;
static void write_callback(struct SoundIoOutputDevice *output_device, int requested_frame_count) {
//device->bytes_per_frame;
float float_sample_rate = output_device->device->default_sample_rate;
float seconds_per_frame = 1.0f / float_sample_rate;
while (requested_frame_count > 0) {
char *data;
int frame_count = requested_frame_count;
soundio_output_device_begin_write(output_device, &data, &frame_count);
// clear everything to 0
memset(data, 0, frame_count * output_device->bytes_per_frame);
const struct SoundIoChannelLayout *channel_layout = &output_device->device->channel_layout;
float *ptr = (float *)data;
// 69 is A 440
float pitch = 440.0f;
float radians_per_second = pitch * 2.0f * PI;
for (int frame = 0; frame < frame_count; frame += 1) {
float sample = sinf((seconds_offset + frame * seconds_per_frame) * radians_per_second);
for (int channel = 0; channel < channel_layout->channel_count; channel += 1) {
*ptr += sample;
ptr += 1;
}
}
seconds_offset += seconds_per_frame * frame_count;
soundio_output_device_write(output_device, data, frame_count);
requested_frame_count -= frame_count;
}
}
static void underrun_callback(struct SoundIoOutputDevice *device) {

View file

@ -287,21 +287,21 @@ int soundio_output_device_create(struct SoundIoDevice *device,
void (*write_callback)(struct SoundIoOutputDevice *, int frame_count),
void (*underrun_callback)(struct SoundIoOutputDevice *),
struct SoundIoOutputDevice **out_output_device);
void soundio_output_device_destroy(struct SoundIoOutputDevice *device);
void soundio_output_device_destroy(struct SoundIoOutputDevice *output_device);
int soundio_output_device_start(struct SoundIoOutputDevice *device);
int soundio_output_device_start(struct SoundIoOutputDevice *output_device);
void soundio_output_device_fill_with_silence(struct SoundIoOutputDevice *device);
void soundio_output_device_fill_with_silence(struct SoundIoOutputDevice *output_device);
// number of frames available to write
int soundio_output_device_free_count(struct SoundIoOutputDevice *device);
void soundio_output_device_begin_write(struct SoundIoOutputDevice *device,
int soundio_output_device_free_count(struct SoundIoOutputDevice *output_device);
void soundio_output_device_begin_write(struct SoundIoOutputDevice *output_device,
char **data, int *frame_count);
void soundio_output_device_write(struct SoundIoOutputDevice *device,
void soundio_output_device_write(struct SoundIoOutputDevice *output_device,
char *data, int frame_count);
void soundio_output_device_clear_buffer(struct SoundIoOutputDevice *device);
void soundio_output_device_clear_buffer(struct SoundIoOutputDevice *output_device);
@ -311,14 +311,14 @@ int soundio_input_device_create(struct SoundIoDevice *device,
enum SoundIoSampleFormat sample_format, double latency, void *userdata,
void (*read_callback)(struct SoundIoOutputDevice *),
struct SoundIoOutputDevice **out_input_device);
void soundio_input_device_destroy(struct SoundIoOutputDevice *device);
void soundio_input_device_destroy(struct SoundIoOutputDevice *input_device);
int soundio_input_device_start(struct SoundIoOutputDevice *device);
void soundio_input_device_peek(struct SoundIoOutputDevice *device,
int soundio_input_device_start(struct SoundIoOutputDevice *input_device);
void soundio_input_device_peek(struct SoundIoOutputDevice *input_device,
const char **data, int *frame_count);
void soundio_input_device_drop(struct SoundIoOutputDevice *device);
void soundio_input_device_drop(struct SoundIoOutputDevice *input_device);
void soundio_input_device_clear_buffer(struct SoundIoOutputDevice *device);
void soundio_input_device_clear_buffer(struct SoundIoOutputDevice *input_device);
#ifdef __cplusplus
}