• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*---------------------------------------------------------------------------*
2  *  audioinwrapper.cpp                                                       *
3  *                                                                           *
4  *  Copyright 2007, 2008 Nuance Communciations, Inc.                         *
5  *                                                                           *
6  *  Licensed under the Apache License, Version 2.0 (the 'License');          *
7  *  you may not use this file except in compliance with the License.         *
8  *                                                                           *
9  *  You may obtain a copy of the License at                                  *
10  *      http://www.apache.org/licenses/LICENSE-2.0                           *
11  *                                                                           *
12  *  Unless required by applicable law or agreed to in writing, software      *
13  *  distributed under the License is distributed on an 'AS IS' BASIS,        *
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15  *  See the License for the specific language governing permissions and      *
16  *  limitations under the License.                                           *
17  *                                                                           *
18  *---------------------------------------------------------------------------*/
19 
20 
21 #if defined(ANDROID) && (defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_4__))
22 
23 //#define USE_DEV_EAC_FILE 1
24 
25 #if defined(USE_DEV_EAC_FILE)
26 #include <fcntl.h>
27 #define N_CHANNELS 1
28 #else
29 #include <media/AudioRecord.h>
30 #include <media/mediarecorder.h>
31 using namespace android;
32 #endif
33 
34 #endif // defined(ANDROID) && (defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_4__))
35 
36 #include "plog.h"
37 
38 // #define SAVE_RAW_AUDIO              1
39 
40 #ifdef SAVE_RAW_AUDIO
41 #include <sys/time.h>
42 #include <stdio.h>
43 
44 
45 static FILE *audio_data;
46 static struct timeval buffer_save_audio;
47 #endif
48 
49 
50 extern "C"
51 {
52 
53 #if defined(ANDROID) && (defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_4__))
54 
55 #if defined(USE_DEV_EAC_FILE)
56 static int audiofd = -1;
57 #else
58 static AudioRecord* record;
59 static int sampleRate = 8000;
60 static int numChannels = 1;
61 #endif
62 
63 // called before AudioOpen
AudioSetInputFormat(int sample_rate,int channel_count)64 int AudioSetInputFormat(int sample_rate, int channel_count)
65 {
66 #if defined(USE_DEV_EAC_FILE)
67   return 0;
68 #else
69   sampleRate = sample_rate;
70   numChannels = channel_count;
71   return 0;
72 #endif
73 }
74 
AudioOpen(void)75 int AudioOpen(void)
76 {
77 #if defined(USE_DEV_EAC_FILE)
78   audiofd = open("/dev/eac", O_RDONLY, 0666);
79   if (audiofd >= 0) {
80     //fcntl(audiofd, F_SETFL, O_NONBLOCK);
81 
82     // possibly lame attempt to get Sooner audio input working
83     struct { unsigned long param1, param2, param3; } params = { 11025, 0, 0 };
84     ioctl(audiofd, 317, &params, sizeof(params));
85   }
86 
87   return audiofd;
88 #else
89     #ifdef SAVE_RAW_AUDIO
90         char file_name [256];
91 
92         gettimeofday ( &buffer_save_audio, NULL );
93         sprintf ( file_name, "data_%ld_%ld.raw", buffer_save_audio.tv_sec, buffer_save_audio.tv_usec );
94         audio_data = fopen ( file_name, "w" );
95     #endif
96 // TODO: get record buffer size from hardware.
97     record = new android::AudioRecord(
98                             android::AUDIO_SOURCE_DEFAULT,
99                             sampleRate,
100                             android::AudioSystem::PCM_16_BIT,
101                             (numChannels > 1) ? android::AudioSystem::CHANNEL_IN_STEREO : android::AudioSystem::CHANNEL_IN_MONO,
102                             8*1024,
103                             0);
104 
105   if (!record) return -1;
106 
107   return record->start() == NO_ERROR ? 0 : -1;
108 #endif
109 }
110 
AudioClose(void)111 int AudioClose(void)
112 {
113 #if defined(USE_DEV_EAC_FILE)
114   return close(audiofd);
115 #else
116   record->stop();
117   delete record;
118     #ifdef SAVE_RAW_AUDIO
119         fclose ( audio_data );
120     #endif
121   return 0;
122 #endif
123 }
124 
AudioRead(short * buffer,int frame_count)125 int AudioRead(short *buffer, int frame_count)
126 {
127   int n;
128 #if defined(USE_DEV_EAC_FILE)
129   n = read(audiofd, buffer, frame_count*sizeof(short)*N_CHANNELS);
130   n /= sizeof(short)*N_CHANNELS;
131   return n;
132 #else
133   int nreq = frame_count * sizeof(short);
134   n = record->read(buffer, nreq);
135   if (n > 0) {
136     if (n != nreq) {
137       PLogError ( "AudioRead error: not enough data %d vs %d\n", n, nreq );
138     }
139     n /= sizeof(short);
140   }
141     #ifdef SAVE_RAW_AUDIO
142         if ( n > 0 )
143             fwrite ( buffer, 2, n, audio_data );
144     #endif
145   return n;
146 #endif
147 }
148 
AudioSetVolume(int stream_type,int volume)149 int AudioSetVolume(int stream_type, int volume)
150 {
151 #if defined(USE_DEV_EAC_FILE)
152   return 0;
153 #else
154   return AudioSystem::setStreamVolume(stream_type, volume, 0);
155 #endif
156 }
157 
AudioGetVolume(int stream_type)158 int AudioGetVolume(int stream_type)
159 {
160 #if defined(USE_DEV_EAC_FILE)
161   return 0;
162 #else
163   float v = 0;
164   AudioSystem::getStreamVolume(stream_type, &v, 0);
165   return int(v * 100.0f);
166 #endif
167 }
168 
169 #else
170 
171 int AudioOpen(void)
172 {
173   return -1;
174 }
175 
176 int AudioClose(void)
177 {
178   return -1;
179 }
180 
181 int AudioSetInputFormat(int sample_rate, int channel_count)
182 {
183   return -1;
184 }
185 
186 int AudioSetOutputFormat(int sample_rate, int channel_count)
187 {
188   return -1;
189 }
190 
191 int AudioRead(short *buffer, int frame_count)
192 {
193   return -1;
194 }
195 
196 int AudioWrite(short *buffer, int frame_count)
197 {
198   return -1;
199 }
200 
201 int AudioSetStreamType(int stream_type)
202 {
203   return -1;
204 }
205 
206 int AudioSetVolume(int stream_type, int volume)
207 {
208   return -1;
209 }
210 
211 int AudioGetVolume(int stream_type)
212 {
213   return -1;
214 }
215 
216 #endif
217 
218 } // extern "C"
219