00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <pthread.h>
00024
00025 #include "audio.h"
00026 #include "audio_oss.h"
00027 #include "audio_alsa.h"
00028
00029 static void error_message(char *msg);
00030 static message_handler msg_handler = NULL;
00031
00033 typedef struct ts_audio_device {
00034 pthread_cond_t segment_cond_t[AUDIO_BUFFER_SEGMENTS];
00035 pthread_mutex_t segment_mutex_t[AUDIO_BUFFER_SEGMENTS];
00037 ts_audio_pub audio_pub;
00039 p_audio_api audio_api;
00040 } ts_audio_device;
00041
00042 static const ts_audio_api AUDIO_DEVICE_TABLE[] = {
00044 {
00045 .audio_device_type_name = "OSS",
00046 .open = audio_open_oss,
00047 .init = audio_init_oss,
00048 .close = audio_close_oss,
00049 .play = audio_play_oss,
00050 .record = audio_record_oss
00051 },
00053 {
00054 .audio_device_type_name = "Alsa",
00055 .open = audio_open_alsa,
00056 .init = audio_init_alsa,
00057 .close = audio_close_alsa,
00058 .play = audio_play_alsa,
00059 .record = audio_record_alsa
00060 },
00061
00062 AUDIO_DEVICE_TEMINATOR
00063 };
00064 #define AUDIO_NUMBER_OF_DEVICE_TYPES ( (sizeof(AUDIO_DEVICE_TABLE) / sizeof(AUDIO_DEVICE_TABLE[0])) - 1)
00065
00066 p_audio_device audio_create(uint32_t device_type,
00067 te_audio_rec_play rec_play
00068 )
00069 {
00070 p_audio_device tmp = NULL;
00071 uint32_t i;
00072
00073 if(NULL != (tmp = malloc(sizeof(ts_audio_device))) ) {
00074
00075
00076
00077 if(device_type < AUDIO_NUMBER_OF_DEVICE_TYPES) {
00078
00079 tmp->audio_api = (p_audio_api)&AUDIO_DEVICE_TABLE[device_type];
00080
00081 tmp->audio_pub.error_msg_handler = error_message;
00082
00083 tmp->audio_pub.play_record = rec_play;
00084
00085 tmp->audio_pub.audio_buffer_raw = NULL;
00086
00087 for(i = 0; i < AUDIO_BUFFER_SEGMENTS; i++) {
00088 pthread_cond_init(&tmp->segment_cond_t[i], NULL);
00089 pthread_mutex_init(&tmp->segment_mutex_t[i], NULL);
00090 }
00091 }
00092 else {
00093
00094 free(tmp);
00095 tmp = NULL;
00096 }
00097 }
00098 return tmp;
00099 }
00100
00101 int32_t audio_open(p_audio_device device, int32_t sample_frequency)
00102 {
00103 int32_t tmp;
00104
00105 if(NULL != device) {
00106
00107 device->audio_pub.sample_frequency = sample_frequency;
00108 tmp = device->audio_api->open(&device->audio_pub);
00109 tmp = device->audio_api->init(&device->audio_pub);
00110 }
00111 else {
00112 tmp = -1;
00113 }
00114 return tmp;
00115 }
00116
00117 int32_t audio_close(p_audio_device device)
00118 {
00119 if(NULL != device) {
00120 return device->audio_api->close(&device->audio_pub);
00121 }
00122 else {
00123 return -1;
00124 }
00125 }
00126
00127 int32_t audio_init(p_audio_device device)
00128 {
00129 if(NULL != device) {
00130 return device->audio_api->init(&device->audio_pub);
00131 }
00132 else {
00133 return -1;
00134 }
00135 }
00136
00137 int32_t audio_destroy(p_audio_device *device)
00138 {
00139 if(NULL != device) {
00140 if(NULL != *device) {
00141 (void)(*device)->audio_api->close(&(*device)->audio_pub);
00142 free((*device)->audio_pub.audio_buffer_raw);
00143 free(*device);
00144 *device = NULL;
00145 }
00146 }
00147 return 0;
00148 }
00149
00150 void audio_capture(p_audio_device device, te_audio_segments buffer_segment)
00151 {
00152 if(NULL != device) {
00153 device->audio_api->record(&device->audio_pub, buffer_segment);
00154 pthread_cond_signal(&device->segment_cond_t[buffer_segment]);
00155 }
00156 }
00157
00158 void audio_capture_wait(p_audio_device device, te_audio_segments buffer_segment)
00159 {
00160 if(NULL != device) {
00161 pthread_cond_wait(&device->segment_cond_t[buffer_segment], &device->segment_mutex_t[buffer_segment]);
00162 }
00163 }
00164
00165 void audio_play(p_audio_device device, te_audio_segments buffer_segment)
00166 {
00167 if(NULL != device) {
00168 device->audio_api->play(&device->audio_pub, buffer_segment);
00169 }
00170 }
00171
00172 void audio_raw2double(p_audio_device device, te_audio_segments buffer_segment, double *audio_data)
00173 {
00174 int32_t i;
00175 int32_t start, end, step;
00176 uint8_t *ptmp_u8;
00177 int16_t tmp_s16;
00178 double *ptmp_d = audio_data;
00179
00180 if(NULL != device) {
00181 start = device->audio_pub.audio_buffer_size * buffer_segment;
00182 ptmp_u8 = &device->audio_pub.audio_buffer_raw[start];
00183
00184 end = start + device->audio_pub.audio_buffer_size;
00185 step = 2 * (device->audio_pub.dsp_channels + 1);
00186
00187 for(i = start; i < end; i += step) {
00188 tmp_s16 = (int16_t)(ptmp_u8[0] + (ptmp_u8[1] << 8));
00189 *ptmp_d = (double)tmp_s16;
00190 ptmp_d++;
00191 ptmp_u8 += step;
00192 }
00193 pthread_mutex_unlock(&device->segment_mutex_t[buffer_segment]);
00194 }
00195 }
00196
00197 void audio_double2raw(p_audio_device device, te_audio_segments buffer_segment, double *audio_data)
00198 {
00199 int32_t i;
00200 int32_t start, end, step;
00201 int16_t ptmp_s16;
00202 uint8_t *ptmp_u8;
00203 double *ptmp_d = audio_data;
00204
00205 if(NULL != device) {
00206 start = device->audio_pub.audio_buffer_size * buffer_segment;
00207 ptmp_u8 = &device->audio_pub.audio_buffer_raw[start];
00208
00209 end = start + device->audio_pub.audio_buffer_size;
00210 step = 2 * (device->audio_pub.dsp_channels + 1);
00211
00212 for(i = start; i < end; i += step) {
00213 ptmp_s16 = (int16_t)*ptmp_d++ & 0xFFFF;
00214 *ptmp_u8++ = (uint8_t)ptmp_s16 & 0xFF;
00215 *ptmp_u8++ = (uint8_t)(ptmp_s16 >> 8) & 0xFF;
00216 }
00217 }
00218 }
00219
00220 uint32_t audio_device_samples_get(p_audio_device device)
00221 {
00222 uint32_t tmp;
00223
00224 tmp = 0;
00225 if(NULL != device) {
00226 tmp = device->audio_pub.samples;
00227 }
00228 return tmp;
00229 }
00230
00231 static void error_message(char *msg)
00232 {
00233 if(msg_handler == NULL) {
00234 printf("%s\n", msg);
00235 }
00236 else {
00237 msg_handler(msg);
00238 }
00239 }
00240
00241
00242
00243
00244 int32_t audio_supported_device_types_get(void)
00245 {
00246 return AUDIO_NUMBER_OF_DEVICE_TYPES;
00247 }
00248
00249 char* audio_device_type_name_get(int32_t audio_device_type_id)
00250 {
00251 return AUDIO_DEVICE_TABLE[audio_device_type_id].audio_device_type_name;
00252 }
00253
00254
00255
00256
00257 void audio_device_name_set(p_audio_device device, const char *name)
00258 {
00259 if(NULL != device) {
00260 snprintf(device->audio_pub.name, AUDIO_DEVICE_NAME_LENGTH, name);
00261 }
00262 }
00263
00264 void audio_device_name_get(p_audio_device device, char **name)
00265 {
00266 if(NULL != device) {
00267 *name = device->audio_pub.name;
00268 }
00269 }
00270
00271
00272
00273
00274 void audio_sample_rate_set(p_audio_device device, int32_t Hertz)
00275 {
00276 if(NULL != device) {
00277 device->audio_pub.sample_frequency = Hertz;
00278 }
00279 }
00280
00281 int32_t audio_sample_rate_get(p_audio_device device)
00282 {
00283 int32_t tmp;
00284
00289 if(NULL != device) {
00290 tmp = device->audio_pub.sample_frequency;
00291 }
00292 else {
00293 tmp = 0;
00294 }
00295 return tmp;
00296 }
00297
00298
00299 void register_error_message_handler(message_handler handler)
00300 {
00301 msg_handler = handler;
00302 }