36 |
#include <fcntl.h> |
#include <fcntl.h> |
37 |
#include <sys/soundcard.h> |
#include <sys/soundcard.h> |
38 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
39 |
|
#include <iostream> |
40 |
|
|
41 |
#include "record.hh" |
#include "record.hh" |
42 |
|
|
45 |
|
|
46 |
char audio_name[20] = "/dev/dsp"; |
char audio_name[20] = "/dev/dsp"; |
47 |
|
|
48 |
int record () |
int record (const char *file) |
49 |
{ |
{ |
50 |
int prof = APF_NORMAL; |
// int prof = APF_NORMAL; |
51 |
int timelimit = 2; |
// int timelimit = 2; |
52 |
int dsp_speed = 11025; |
// int dsp_speed = 11025; |
53 |
int dsp_stereo = 0; |
// int dsp_stereo = 0; |
54 |
int samplesize = 16; |
// int samplesize = 16; |
55 |
int audio, abuf_size; |
// int audio, abuf_size; |
56 |
int blksize=0; |
// int blksize=0; |
57 |
int fragsize=0; |
// int fragsize=0; |
58 |
int tmp; |
// int tmp; |
59 |
int fd, l; |
// int fd, l; |
60 |
int count, ch; |
// int count, ch; |
61 |
char *audiobuf; |
// char *audiobuf; |
62 |
char *file=("/tmp/tmp000_1"); |
|
63 |
|
// audio = open (audio_name, O_RDONLY, 0); |
64 |
audio = open (audio_name, O_RDONLY, 0); |
// if (audio == -1) |
65 |
if (audio == -1) |
// { |
66 |
{ |
// perror (audio_name); |
67 |
perror (audio_name); |
// return 1; |
68 |
return 1; |
// } |
69 |
} |
|
70 |
|
// if (fragsize != 0) |
71 |
if (fragsize != 0) |
// if (ioctl(audio, SNDCTL_DSP_SETFRAGMENT, &fragsize)==-1) |
72 |
if (ioctl(audio, SNDCTL_DSP_SETFRAGMENT, &fragsize)==-1) |
// { |
73 |
{ |
// perror("SETFRAGMENT"); |
74 |
perror("SETFRAGMENT"); |
// return 1; |
75 |
return 1; |
// } |
76 |
} |
|
77 |
|
// tmp = samplesize; |
78 |
tmp = samplesize; |
// ioctl(audio, SNDCTL_DSP_SAMPLESIZE, &samplesize); |
79 |
ioctl(audio, SNDCTL_DSP_SAMPLESIZE, &samplesize); |
// if (tmp != samplesize) |
80 |
if (tmp != samplesize) |
// { |
81 |
{ |
// fprintf(stderr, "Unable to set the sample size\n"); |
82 |
fprintf(stderr, "Unable to set the sample size\n"); |
// return 1; |
83 |
return 1; |
// } |
84 |
} |
|
85 |
|
// ioctl(audio, SNDCTL_DSP_PROFILE, &prof); |
86 |
ioctl(audio, SNDCTL_DSP_PROFILE, &prof); |
|
87 |
|
// if (ioctl (audio, SNDCTL_DSP_STEREO, &dsp_stereo)==-1) |
88 |
if (ioctl (audio, SNDCTL_DSP_STEREO, &dsp_stereo)==-1) |
// { |
89 |
{ |
// perror (audio_name); |
90 |
perror (audio_name); |
// return 1; |
91 |
return 1; |
// } |
|
} |
|
92 |
|
|
93 |
if (ioctl (audio, SNDCTL_DSP_SPEED, &dsp_speed) == -1) |
// if (ioctl (audio, SNDCTL_DSP_SPEED, &dsp_speed) == -1) |
94 |
{ |
// { |
95 |
perror (audio_name); |
// perror (audio_name); |
96 |
return 1; |
// return 1; |
97 |
} |
// } |
98 |
|
|
99 |
if (blksize > 0) |
// if (blksize > 0) |
100 |
abuf_size = blksize; |
// abuf_size = blksize; |
101 |
else |
// else |
102 |
{ |
// { |
103 |
ioctl (audio, SNDCTL_DSP_GETBLKSIZE, &abuf_size); |
// ioctl (audio, SNDCTL_DSP_GETBLKSIZE, &abuf_size); |
104 |
if (abuf_size < 1) |
// if (abuf_size < 1) |
105 |
{ |
// { |
106 |
perror ("GETBLKSIZE"); |
// perror ("GETBLKSIZE"); |
107 |
return 1; |
// return 1; |
108 |
} |
// } |
109 |
} |
// } |
110 |
|
|
111 |
if ((audiobuf = (char *)malloc (abuf_size)) == NULL) |
// if ((audiobuf = (char *)malloc (abuf_size)) == NULL) |
112 |
{ |
// { |
113 |
fprintf (stderr, "Unable to allocate input/output buffer\n"); |
// fprintf (stderr, "Unable to allocate input/output buffer\n"); |
114 |
return 1; |
// return 1; |
115 |
} |
// } |
116 |
|
|
117 |
if (!timelimit) |
// if (!timelimit) |
118 |
count = 0x7fffffff; |
// count = 0x7fffffff; |
119 |
else |
// else |
120 |
{ |
// { |
121 |
count = timelimit * dsp_speed; |
// count = timelimit * dsp_speed; |
122 |
if (dsp_stereo) |
// if (dsp_stereo) |
123 |
count *= 2; |
// count *= 2; |
124 |
if (samplesize != 8) |
// if (samplesize != 8) |
125 |
count *= 2; |
// count *= 2; |
126 |
} |
// } |
127 |
|
|
128 |
if (!file) |
// if (!file) |
129 |
{ |
// { |
130 |
fd = 1; |
// fd = 1; |
131 |
file = "stdout"; |
// file = "stdout"; |
132 |
} |
// } |
133 |
else |
// else |
134 |
{ |
// { |
135 |
if ((fd = open (file, O_WRONLY | O_CREAT, 0666)) == -1) |
// if ((fd = open (file, O_WRONLY | O_CREAT, 0666)) == -1) |
136 |
{ |
// { |
137 |
perror (file); |
// perror (file); |
138 |
return 1; |
// return 1; |
139 |
} |
// } |
140 |
} |
// } |
141 |
|
|
142 |
while (count) |
// while (count) |
143 |
{ |
// { |
144 |
ch = count; |
// ch = count; |
145 |
if (ch > abuf_size) |
// if (ch > abuf_size) |
146 |
ch = abuf_size; |
// ch = abuf_size; |
147 |
|
|
148 |
if ((l = read (audio, audiobuf, ch)) > 0) |
// if ((l = read (audio, audiobuf, ch)) > 0) |
149 |
{ |
// { |
150 |
if (write (fd, audiobuf, l) != l) |
// if (write (fd, audiobuf, l) != l) |
151 |
{ |
// { |
152 |
perror (file); |
// perror (file); |
153 |
return 1; |
// return 1; |
154 |
} |
// } |
155 |
count -= l; |
// count -= l; |
156 |
} |
// } |
157 |
|
|
158 |
if (l == -1) |
// if (l == -1) |
159 |
{ |
// { |
160 |
perror (audio_name); |
// perror (audio_name); |
161 |
return 1; |
// return 1; |
162 |
} |
// } |
163 |
} |
// } |
164 |
|
|
165 |
if (fd != 1) |
// if (fd != 1) |
166 |
close (fd); |
// close (fd); |
167 |
|
|
168 |
close (audio); |
// close (audio); |
169 |
|
|
170 |
|
std::string cmd = "brec -s11025 -b16 -w -t 2 "; |
171 |
|
cmd += file; |
172 |
|
cmd += " 1> /dev/null 2> /dev/null"; |
173 |
|
|
174 |
|
system(cmd.c_str()); |
175 |
|
|
176 |
return 0; |
return 0; |
177 |
} |
} |