All pastes #2084529 Raw Edit

Stars

public c v1 · immutable
#2084529 ·published 2011-09-29 04:35 UTC
rendered paste body
#include <ao/ao.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <curses.h>#include "aotest.h"#include "mix.c"#include "wave.c"#define BIT_DEPTH	16#define SAMPLE_RATE	44100#define CHANNELS	2#define FMT_MATRIX	NULL#define BUFFER_LEN  8      /*buffer length in ms*/#define SAMPLE_SIZE	(BIT_DEPTH * CHANNELS)	/* the size of one sample is the bit depth * the number of channels */#define BUFFER_SIZE ((SAMPLE_SIZE * SAMPLE_RATE) / 1000) * BUFFER_LEN	/* the buffer size is the sample size times the sample rate (which will give you a 1000ms/1s buffer) / 1000 * BUFFER_LEN */#define BLOCK_SIZE (SAMPLE_SIZE * 8) /* the size of a ``block'' of audio - here it will be 8 samples */ao_sample_format format = {	BIT_DEPTH,	SAMPLE_RATE,	CHANNELS,	AO_FMT_LITTLE,	FMT_MATRIX};typedef struct {			/* implement a (very lightweight) circular buffer for the audio buffer */	uint64_t write_ptr;	uint64_t read_ptr;	uint64_t size;	uint64_t buf_ptr[0];	/* pointer to actual buffer data */} buffer_t;int16_t read_buffer(buffer_t *buffer) {	int16_t bee = buffer->buf_ptr[buffer->read_ptr];	buffer->buf_ptr[buffer->read_ptr] = 0;	/* we don't want to cycle through the buffer and read the same data again do we */	buffer->read_ptr++;	buffer->read_ptr %= buffer->size;	return bee;}void write_buffer(buffer_t *buffer, int16_t x) { /* this is void because we don't really need to know if we overwrote anything */	buffer->buf_ptr[buffer->write_ptr] = x;	buffer->read_ptr++;	buffer->write_ptr %= buffer->size;}int main () {	printf("%s", "hello there!\n");	ao_device *device;	int driver, i;    char *input;    char c;		buffer_t ao_buffer;	ao_buffer.size = BUFFER_SIZE;	ao_buffer.buf_ptr[0] = calloc(BUFFER_SIZE, SAMPLE_SIZE);	printf("%s", "we just initialized the buffer\n");		ao_initialize();	driver = ao_default_driver_id();	device = ao_open_live(driver, &format, NULL);	printf("%s", "we just initalized the audio device\n");	if (device == NULL) {		printf("oh no device is null");		return 1;	}		printf("%s", "we are about to enter the Main Loop! :O\n");	while (c != 'x') {    while (c != 'x') {             /* main loop ! */		if (!ao_play(device, read_buffer(&ao_buffer), BLOCK_SIZE)) {			printf("device failure: ao_play returned 0\n");			c = 'x';			break;		}		c = getch();		if (c == 's') {			char *bee = generate_sine_wave(440.0);			for (i=0; i < BUFFER_SIZE; i++)				write_buffer(&ao_buffer, bee[i]);		}        else if (c == 'x') {					}    }	}		printf("%s", "done here\n");	    free(&ao_buffer);	ao_close(device);	ao_shutdown();		return 0;}