forked from cryptix/popcorn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
audioout.c
75 lines (59 loc) · 1.47 KB
/
audioout.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Now using libao - http://www.xiph.org/ao/doc/
*/
#include <stdio.h>
#include <ao/ao.h>
#include <pthread.h>
#include "audioout.h"
#include "voices.h"
ao_device *aoInit(ao_sample_format *fmt) {
ao_device *dev;
int def_driver;
/* init */
ao_initialize();
def_driver = ao_default_driver_id();
/* open device */
dev = ao_open_live(def_driver, fmt, NULL);
if(dev == NULL) {
fprintf(stderr, "Error opening device.\n");
return NULL;
}
return dev;
}
void aoMix(void *parameters) {
extern int running; /* global running state */
aoMixParam *para;
int rate, i, tmp;
float sample;
/* buffer */
char *buf;
int bufLen;
para = (aoMixParam *) parameters;
bufLen = para->fmt->bits / 8 * para->fmt->channels * (rate = para->fmt->rate/250);
buf = (char *) calloc(bufLen, sizeof(char));
if(buf == NULL) {
fprintf(stderr, "Error: Couldn't allocate audio buffer memory.\n");
perror("calloc");
pthread_exit(NULL);
}
while(running) { /* i hate globals */
for (i=0; i<rate; i++) {
sample = compVoices() * 64; /* gain */
/* clip the result
* this looks like a crude method, but works very well
*/
if (sample > 127) sample = 127;
if (sample < -128) sample = -128;
tmp = (int) sample;
/* assuming 16bits */
buf[4*i] = buf[4*i+2] = tmp & 0xff;
buf[4*i+1] = buf[4*i+3] = (tmp >> 8) & 0xff;
}
if(ao_play(para->dev, buf, bufLen) == 0) running = 0;
}
pthread_exit(NULL);
}
void aoClose(ao_device *dev) {
ao_close(dev);
ao_shutdown();
}