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
|
// based on https://gist.github.com/albanpeignier/104902
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <time.h>
#include <limits.h>
#include <fcntl.h>
#include <alsa/asoundlib.h>
#include <math.h>
unsigned int rate = 44100;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
snd_pcm_uframes_t buffer_size = 44100 * 2;
snd_pcm_uframes_t period_size = 44100;
volatile sig_atomic_t isStop = 0;
void stopper(int sig, siginfo_t *info, void *ctx)
{
isStop = 1;
}
void PCMCallBack(snd_async_handler_t *pcm_callback)
{
snd_pcm_t *pcm_handle = snd_async_handler_get_pcm(pcm_callback);
int16_t **ptr = snd_async_handler_get_callback_private(pcm_callback);
int err;
int16_t *tptr;
snd_pcm_sframes_t delay;
snd_pcm_delay(pcm_handle,&delay);
while (delay >= period_size)
{
tptr = bufferRing_next(); //バッファを進める関数(ここでは未実装)
snd_pcm_readi(pcm_handle, tptr, period_size);
*ptr = tptr;
delay = snd_pcm_delay(pcm_handle,&delay);
}
}
int init_audioif(snd_pcm_t *handle)
{
int err;
snd_pcm_hw_params_t *params;
if ((err = snd_pcm_hw_params_malloc(¶ms)) < 0)
{
fprintf(stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_any(handle, params)) < 0)
{
fprintf(stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{
fprintf(stderr, "cannot set access type (%s)\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_format(handle, params, format)) < 0)
{
fprintf(stderr, "cannot set sample format (%s)\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_rate_near(handle, params, &rate, 0)) < 0)
{
fprintf(stderr, "cannot set sample rate (%s)\n", snd_strerror(err));
return 1;
}
//下2項目が追加
if ((err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer_size)) < 0)
{
fprintf(stderr, "cannot set buffer size (%s)\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_period_size_near(handle, params, &period_size, NULL)) < 0)
{
fprintf(stderr, "cannot set period size (%s)\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_channels(handle, params, 1)) < 0)
{
fprintf(stderr, "cannot set channel count (%s)\n", snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params(handle, params)) < 0)
{
fprintf(stderr, "cannot set parameters (%s)\n", snd_strerror(err));
return 1;
}
snd_pcm_hw_params_free(params);
return 0;
}
int main(int argc, char *argv[])
{
int i, j, err;
snd_pcm_t *capture_handle;
snd_async_handler_t *pcm_callback;
int buffer_count = rate; // 1s
struct sigaction sa_sigabrt;
uint16_t detect_status = 0;
uint16_t *ptrBuf = NULL, *previoud_ptrBuf = NULL;
time_t t_time;
memset(&sa_sigabrt, 0, sizeof(sa_sigabrt));
sa_sigabrt.sa_sigaction = stopper;
sa_sigabrt.sa_flags = SA_SIGINFO;
if (sigaction(SIGINT, &sa_sigabrt, NULL) < 0)
{
fprintf(stderr, "failed to set SIGINIT handler\n");
exit(1);
}
if ((err = snd_pcm_open(&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0)
{
fprintf(stderr, "cannot open audio device %s (%s)\n", argv[1], snd_strerror(err));
exit(1);
}
if (init_audioif(capture_handle) != 0)
{
exit(1);
}
if ((err = snd_pcm_prepare(capture_handle)) < 0)
{
fprintf(stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror(err));
exit(1);
}
snd_async_add_pcm_handler(&pcm_callback, capture_handle, PCMCallBack, &ptrBuf);
snd_pcm_start(capture_handle);
while (isStop == 0)
{
if (ptrBuf == previoud_ptrBuf) //バッファが進んでいるかチェック,このソースでは未実装
{
usleep(500e3);
continue;
}
previoud_ptrBuf = ptrBuf;
int16_t *buffer = previoud_ptrBuf;
for (j = 0; j < buffer_count; j++)
{
in[j][0] = buffer[j] / 100.0;
in[j][1] = 0.0;
}
}
printf("Interrupt...\n");
if (isRecording)
stopRec();
snd_pcm_drain(capture_handle);
snd_async_del_handler(pcm_callback);
snd_pcm_close(capture_handle);
exit(0);
}
|