/
playaiff7.c
127 lines (97 loc) · 2.57 KB
/
playaiff7.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Converting a 1-channel stream to 2-channels
* compile with "gcc -o playaiff7 playaiff7.c -lao -ldl -lm -lsndfile"
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ao/ao.h>
#include <sndfile.h>
#define BUFFSIZE 512
void stereoize(short *, short *, size_t);
int playfile(FILE *);
int main(int argc, char *argv[])
{
FILE *fp;
if (argc < 1) {
printf("usage: %s <filename>\n", argv[0]);
exit(1);
}
fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("Cannot open %s.\n", argv[1]);
exit(2);
}
playfile(fp);
fclose(fp);
return 0;
}
int playfile(FILE *fp)
{
int default_driver;
int frames_read;
int count;
int toread;
int readnow;
short *buffer;
short *groovy;
long filestart;
ao_device *device;
ao_sample_format format;
SNDFILE *sndfile;
SF_INFO sf_info;
ao_initialize();
default_driver = ao_default_driver_id();
sf_info.format = 0;
filestart = ftell(fp);
sndfile = sf_open_fd(fileno(fp), SFM_READ, &sf_info, 0);
memset(&format, 0, sizeof(ao_sample_format));
format.byte_format = AO_FMT_NATIVE;
format.bits = 16;
format.rate = sf_info.samplerate;
// format.channels = sf_info.channels;
format.channels = 2;
printf("Channels: %d\n", sf_info.channels);
printf("Samplerate: %d\n", sf_info.samplerate);
device = ao_open_live(default_driver, &format, NULL);
if (device == NULL) {
printf("Error opening sound device.\n");
return 1;
}
buffer = malloc(BUFFSIZE * sf_info.channels * sizeof(short));
groovy = malloc(BUFFSIZE * 2 * sizeof(short));
frames_read = 0;
toread = sf_info.frames * sf_info.channels;
while (toread > 0) {
if (toread < BUFFSIZE * sf_info.channels)
count = toread;
else
count = BUFFSIZE * sf_info.channels;
frames_read = sf_read_short(sndfile, buffer, count);
if (sf_info.channels == 1)
stereoize(groovy, buffer, count * sizeof(short));
else
memcpy(groovy, buffer, count * sizeof(short));
ao_play(device, (char *)groovy, frames_read * sizeof(short));
toread = toread - frames_read;
}
free(buffer);
free(groovy);
fseek(fp, filestart, SEEK_SET);
ao_close(device);
sf_close(sndfile);
ao_shutdown();
printf("Finished\n");
return 0;
}
void stereoize(short *outbuf, short *inbuf, size_t length)
{
int count;
int outcount;
outcount = 0;
for (count = 0; count <= length; count++) {
outbuf[outcount] = outbuf[outcount+1] = inbuf[count];
outcount += 2;
}
}