-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_sound.c
160 lines (127 loc) · 2.5 KB
/
get_sound.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
#include <fcntl.h>
#include <linux/soundcard.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include "get_sound.h"
static int setup_dsp( int fd );
int recoard_sound(short *, int);
int check_warmup(short *, int);
int get_sound(short *buf, int rtime)
{
int flag = 0;
int output_count = 0;
int i, ret;
unsigned int buffer_size;
if(! buf){
return RET_ERROR;
}
buffer_size = BUFSIZE * rtime;
ret = recoard_sound(buf, buffer_size * sizeof(short));
if(ret == RET_ERROR){
return RET_ERROR;
}
for(i=0;i<buffer_size/sizeof(short);i++){
ret = check_warmup(buf, i);
if(ret == RET_SUCCESS){
SetFlag(flag, WARMUP_STATE);
}
/*
if(GetFlag(flag, WARMUP_STATE)){
if((++output_count % range) == 0){
//printf("\n");
if((i + range) > (buffer_size/sizeof(short))){
break;
}
}
//printf("%d ", buf[i]);
}
*/
}
return RET_SUCCESS;
}
int check_warmup(short *buf, int current_index)
{
int i;
if(!buf){
return RET_ERROR;
}
if(buf[current_index] == 0){
return RET_ERROR;
}
for(i=current_index;i<(current_index + VIEW_RANGE);i++){
if(buf[i] == 0){
return RET_ERROR;
}
}
return RET_SUCCESS;
}
int recoard_sound(short *buf, int len)
{
int fd;
if ( ( fd = open( "/dev/dsp", O_RDWR ) ) == -1 ) {
perror( "open()" );
return RET_ERROR;
}
if ( setup_dsp( fd ) != 0 ) {
fprintf( stderr, "Setup /dev/dsp failed.\n" );
close( fd );
return RET_ERROR;
}
if ( read(fd, buf, len) == -1 ) {
perror( "read()" );
close( fd );
return RET_ERROR;
}
/*
if ( write( fd, buf, len ) == -1 ) {
perror( "write()" );
close( fd );
return 1;
}
*/
close( fd );
return RET_SUCCESS;
}
void write_sound_log(short *buf, int len)
{
int i;
FILE *fd;
fd = fopen(SoundOUTPUT, "a");
if(! fd){
return ;
}
for(i=0;i<len;i++){
fprintf(fd, "%d\n", buf[i]);
}
fclose(fd);
}
/*
* /dev/dsp を以下の様に設定する。
*
* 量子化ビット数 : 16 bits
* サンプリング周波数 : 44.1 KHz
* チャンネル数 : 1
* PCM データは符号付き、リトルエンディアン
*
*/
static int setup_dsp( int fd )
{
int fmt = AFMT_S16_LE;
int freq = 44100;
int channel = 1;
if ( ioctl( fd, SOUND_PCM_SETFMT, &fmt ) == -1 ) {
perror( "ioctl( SOUND_PCM_SETFMT )" );
return -1;
}
if ( ioctl( fd, SOUND_PCM_WRITE_CHANNELS, &channel ) == -1 ) {
perror( "ioctl( SOUND_PCM_WRITE_CHANNELS )" );
return -1;
}
if ( ioctl( fd, SOUND_PCM_WRITE_RATE, &freq ) == -1 ) {
perror( "ioctl( SOUND_PCM_WRITE_RATE )" );
return -1;
}
return 0;
}