-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_test.c
183 lines (146 loc) · 4.36 KB
/
audio_test.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***************************************************************************
* Copyright (C) 2009 by open-nandra *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define WAV_RIFF ('R' | ('I' << 8) | ('F' << 16) | ('F' << 24))
#define WAV_WAVE ('W' | ('A' << 8) | ('V' << 16) | ('E' << 24))
#define WAV_FMT ('f' | ('m' << 8) | ('t' << 16) | (' ' << 24))
#define WAV_DATA ('d' | ('a' << 8) | ('t' << 16) | ('a' << 24))
#define WAV_HDR_SIZE 44
#define WAV_DATA_LEN_OFFSET (WAV_HDR_SIZE - 4)
#define read_little_end_word_short(p) \
( (*((p) + 0) << 0) | (*((p) + 1) << 8) )
#define read_little_end_word(p) \
( (*((p) + 0) << 0) | (*((p) + 1) << 8) | (*((p) + 2) << 16) | (*((p) + 3) << 24))
unsigned char hdr[WAV_HDR_SIZE];
unsigned char audio_samples [8192];
unsigned char check_wav_header(unsigned char *hdr, unsigned long *size)
{
unsigned char *p = NULL;
unsigned char ret = 0;
p = hdr;
if (read_little_end_word(p) != WAV_RIFF) {
printf("RIFF\n");
goto end;
}
p += 8;
if (read_little_end_word(p) != WAV_WAVE) {
printf("WAV_WAVE\n");
goto end;
}
p += 4;
if (read_little_end_word(p) != WAV_FMT) {
printf("FORMAT\n");
goto end;
}
p += 4;
if (read_little_end_word(p) != 16) { // expect standard fmt chunk (length: 16 bytes)
printf("Length\n");
goto end;
}
p += 4;
p += 2;
p += 2;
p += 4;
p += 4;
p += 2;
p += 2;
if (read_little_end_word(p) != WAV_DATA) {
printf("DATA\n");
goto end;
}
p += 4;
*size = read_little_end_word_short(p);
if (*size == 0)
goto end;
ret = 1;
end:
return ret;
}
int main (int argc, char **argv)
{
int ret = 0;
unsigned long size_global;
unsigned long size = 8192;
int count;
int fd, fd_wav;
const char * const device = "/dev/audio";
int result;
unsigned char buf[4];
if (argc <= 1) {
printf("wav file missisng\n");
goto end;
}
/* open wav file */
printf("%s\n", argv[1]);
fd_wav = open(argv[1], O_RDONLY);
if (fd_wav < 0) {
perror("open");
goto end;
}
if (read(fd_wav, hdr, sizeof(hdr)) != sizeof(hdr)) {
perror("read");
goto end_close;
}
if (!check_wav_header(hdr, &size_global)) {
printf("Error header decoding\n");
goto end_close;
}
fd = open(device, O_WRONLY);
if (fd < 0) {
perror("open");
goto end;
}
while (1) {
result = read(fd_wav, audio_samples, size);
switch (result) {
case 0:
printf("End of file rewind\n");
if (lseek(fd_wav, WAV_DATA_LEN_OFFSET, SEEK_SET) != WAV_DATA_LEN_OFFSET)
goto end_close;
if (read(fd_wav, buf, sizeof(buf)) != sizeof(buf))
goto end_close;
size_global = read_little_end_word(buf);
if (size_global == 0)
goto end_close;
break;
default:
printf("Readed OK size :%d \n", result);
break;
}
// sleep(5);
count = write(fd, audio_samples, sizeof(audio_samples));
if (count <= 0 ) {
perror("write");
break;
} else {
printf("Data successfully written\n");
}
}
end_close:
close(fd_wav);
close(fd);
end:
return ret;
}