[Audio_test]修复了回调方法的实现
1.修复了回调方法的实现
2.修复了录音功能的bug
3.修改了程序选项
Change-Id: Ib8688ddb0966c2d6c40422fac7d7f82a52ed76c3
diff --git a/mbtk/include/mbtk/mbtk_audio.h b/mbtk/include/mbtk/mbtk_audio.h
index 2f286d8..caa458d 100755
--- a/mbtk/include/mbtk/mbtk_audio.h
+++ b/mbtk/include/mbtk/mbtk_audio.h
@@ -17,7 +17,6 @@
typedef void* mbtk_audio_handle;
typedef void(*mbtk_audio_record_cb_func)(int result, char* databuf, unsigned int len); //callback function get recording data
-typedef void(*mbtk_audio_player_cb_func)(int hdl, int result); //callback function get playing data
typedef int(*mbtk_dtmf_cb)(char result);
typedef int(*mbtk_volume_cb)(int result);
typedef uint32 mbtk_audio_client_handle_type;
@@ -98,7 +97,7 @@
int : Return 0 if success,other for failure.
===========================================================================*/
-int mbtk_audio_play_file(mbtk_audio_handle dev_hdl, int file_fd, int offset, mbtk_audio_player_cb_func cb_func);
+int mbtk_audio_play_file(mbtk_audio_handle dev_hdl, int file_fd, int offset);
/*===========================================================================
FUNCTION mbtk_audio_record
diff --git a/mbtk/mbtk_lib/src/mbtk_audio.c b/mbtk/mbtk_lib/src/mbtk_audio.c
index ff58fe9..f026769 100755
--- a/mbtk/mbtk_lib/src/mbtk_audio.c
+++ b/mbtk/mbtk_lib/src/mbtk_audio.c
@@ -59,6 +59,30 @@
PARAM_INT_POLICY_3,
PARAM_INT_POLICY_MAX,
} PARAM_INT_POLICY;
+
+#define FAILURE -1
+#define ID_RIFF 0x46464952
+#define ID_WAVE 0x45564157
+#define ID_FMT 0x20746d66
+#define ID_DATA 0x61746164
+#define FORMAT_PCM 1
+
+struct wav_header {
+ uint32_t riff_id;
+ uint32_t riff_sz;
+ uint32_t riff_fmt;
+ uint32_t fmt_id;
+ uint32_t fmt_sz;
+ uint16_t audio_format;
+ uint16_t num_channels;
+ uint32_t sample_rate;
+ uint32_t byte_rate; /* sample_rate * num_channels * bps / 8 */
+ uint16_t block_align; /* num_channels * bps / 8 */
+ uint16_t bits_per_sample;
+ uint32_t data_id;
+ uint32_t data_sz;
+};
+
const struct blobmsg_policy int_policy[] ={
[PARAM_INT_POLICY_0] = {
.name = "param0",
@@ -105,6 +129,36 @@
#define AUDIO_FILE_DIR "/data"
#define MBTK_AUD_DEMO_WAV "/data/demo.wav"
+int MBTK_wav_pcm16Le_set(int fd)
+{
+ struct wav_header hdr;
+
+ if (fd <= 0)
+ return -1;
+
+ memset(&hdr, 0, sizeof(struct wav_header));
+
+ hdr.riff_id = ID_RIFF;
+ hdr.riff_fmt = ID_WAVE;
+ hdr.fmt_id = ID_FMT;
+ hdr.fmt_sz = 16;
+ hdr.audio_format = FORMAT_PCM;
+ hdr.num_channels = 1;
+ hdr.sample_rate = 8000;
+ hdr.bits_per_sample = 16;
+ hdr.byte_rate = (8000 * 1 * hdr.bits_per_sample) / 8;
+ hdr.block_align = (hdr.bits_per_sample * 1) / 8;
+ hdr.data_id = ID_DATA;
+ hdr.data_sz = 0;
+
+ hdr.riff_sz = hdr.data_sz + 44 - 8;
+ if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+ return -1;
+ }
+
+ return 0;
+}
+
int create_audio_dir(const char *dirname)
{
DIR *p_dir;
@@ -199,15 +253,25 @@
goto err;
}
- if(-1 == mbtk_audio_record(record_hdl, record_cb_func, NULL))
+ if(0 == MBTK_wav_pcm16Le_set(record_fd))// 设置格式,否则音频播放不了
{
- printf("file write error\n");
+ if( 0 != mbtk_audio_record(record_hdl, record_cb_func, NULL))
+ {
+ printf("file write error\n");
+ close(record_fd);
+ record_fd = 0;
+ }
+ }
+ else
+ {
+ printf("arec set file header error\n");
close(record_fd);
record_fd = 0;
}
- sleep(10);
+
+ sleep(10);// 录音 10S
err:
- mbtk_audio_close(record_hdl);
+ mbtk_audio_close(record_hdl);// 关闭录音
record_hdl = NULL;
if(record_fd > 0)
{
diff --git a/mbtk/mbtk_lib/src/mbtk_audio_alsa.c b/mbtk/mbtk_lib/src/mbtk_audio_alsa.c
index 83d2585..d654c6a 100755
--- a/mbtk/mbtk_lib/src/mbtk_audio_alsa.c
+++ b/mbtk/mbtk_lib/src/mbtk_audio_alsa.c
@@ -79,12 +79,6 @@
void *cb_data;
};
-struct player_cb_s
-{
- mbtk_audio_player_cb_func _cb;
- void *cb_data;
-};
-
static struct mopen_audio_t *internal_hdl = NULL;
@@ -306,6 +300,7 @@
{
first_set = 1;
mbtk_audio_set_status(hdl, AUDIO_RUNNING);
+ audio_play_cb(pcxt, AUD_PLAYER_RESUME);
}
pthread_mutex_lock(&pcxt->_stream_mutex);
@@ -316,7 +311,7 @@
if(ret < pcxt->pcm_packet_size)
printf("pcm %d - %d\n", pcxt->pipe_data, ret);
- rc = pcxt->stream_out->write(pcxt->stream_out, data, ret);
+ rc = pcxt->stream_out->write(pcxt->stream_out, data, bufsize);
if (rc < 0) {
printf("%s: error writing (child).\n", __FUNCTION__);
break;
@@ -335,7 +330,7 @@
/* printf("close pcxt->fd!\n"); */
}
if (audio_play_cb)
- audio_play_cb(pcxt, 5);
+ audio_play_cb(pcxt, AUD_PLAYER_FINISHED);
pcxt->pid = 0;
// mbtk_audio_set_status(hdl, AUDIO_STOP);
mbtk_audio_set_status(hdl, AUDIO_OPEN);
@@ -365,7 +360,7 @@
}
if (pcxt->pid == 0) {
if (audio_play_cb)
- audio_play_cb(pcxt, 0);
+ audio_play_cb(pcxt, AUD_PLAYER_START);
mbtk_audio_set_status(pcxt, AUDIO_RUNNING);
ret = pthread_create(&pcxt->pid, NULL, mbtk_play_pthread, hdl);
if (ret != 0)
@@ -625,7 +620,7 @@
}
-int mbtk_audio_play_file(void *dev_hdl, int file_fd, int offset, mbtk_audio_player_cb_func cb_func)
+int mbtk_audio_play_file(void *dev_hdl, int file_fd, int offset)
{
unsigned bufsize = 0;
char *data = NULL;
@@ -635,7 +630,7 @@
int ret;
struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl;
- struct player_cb_s *_usrData = NULL;
+ _play_callback audio_play_cb = (_play_callback)pcxt->usrData;
if (NULL == dev_hdl || NULL == internal_hdl)
return -1;
@@ -643,9 +638,6 @@
return -2;
// file_data_sz = mbtk_wav_pcm16Le_check(file_fd);
- _usrData = malloc(sizeof(struct player_cb_s));
- _usrData->_cb = cb_func;
- _usrData->cb_data = NULL;
bufsize = pcxt->pcm_packet_size;
data = calloc(1, bufsize);
if (!data) {
@@ -679,28 +671,29 @@
while(AUDIO_PAUSE == pcxt->state)
{
usleep(80000);
- _usrData->_cb((int)pcxt, AUD_PLAYER_PAUSE);
+ audio_play_cb(pcxt, AUD_PLAYER_PAUSE);
}
if ((0 == first_set || AUDIO_RESUME == pcxt->state))
{
first_set = 1;
mbtk_audio_set_status(dev_hdl, AUDIO_RUNNING);
- _usrData->_cb((int)pcxt, AUD_PLAYER_RESUME);
+ audio_play_cb(pcxt, AUD_PLAYER_RESUME);
}
ret = pcxt->stream_out->write(pcxt->stream_out, data, bufsize);
if (ret < 0) {
printf("%s: error writing (child).\n", __FUNCTION__);
- _usrData->_cb((int)pcxt, AUD_PLAYER_ERROR);
+ audio_play_cb(pcxt, AUD_PLAYER_ERROR);
break;
} else if (ret < (signed int)pcxt->pcm_packet_size) {
printf("%s: wrote less than buffer size, rc=%d.\n", __FUNCTION__, ret);
- _usrData->_cb((int)pcxt, AUD_PLAYER_LESSDATA);
+ audio_play_cb(pcxt, AUD_PLAYER_LESSDATA);
break;
}
}
- _usrData->_cb((int)pcxt, AUD_PLAYER_FINISHED);
+ if (audio_play_cb)
+ audio_play_cb(pcxt, AUD_PLAYER_FINISHED);
printf("file_data_sz :%d - all_size: %d\n", file_data_sz, all_size);
mbtk_audio_set_status(dev_hdl, AUDIO_OPEN);
diff --git a/mbtk/ql_lib/src/ql_audio.c b/mbtk/ql_lib/src/ql_audio.c
index 4e4dbc9..b94db61 100755
--- a/mbtk/ql_lib/src/ql_audio.c
+++ b/mbtk/ql_lib/src/ql_audio.c
@@ -5,7 +5,6 @@
static mbtk_audio_handle record_hdl = NULL;
static mbtk_audio_handle player_hdl = NULL;
static _cb_onRecorder record_cb_fun = NULL;
-static _cb_onPlayer player_cb_fun = NULL;
static int Samprate = 8000;
/*****************************************************************
@@ -36,7 +35,6 @@
*****************************************************************/
int Ql_AudPlayer_Open(char* device, _cb_onPlayer cb_func)
{
- player_cb_fun = cb_func;
player_hdl = mbtk_audio_open(MBTK_AUTIO_TYPE_OUT, 1, Samprate, cb_func);
return (int)player_hdl;
}
@@ -89,7 +87,7 @@
int Ql_AudPlayer_PlayFrmFile(int hdl, int fd, int offset)
{
- return mbtk_audio_play_file((void *)hdl, fd, offset, player_cb_fun);
+ return mbtk_audio_play_file((void *)hdl, fd, offset);
}
//
diff --git a/mbtk/test/audio_test.c b/mbtk/test/audio_test.c
index 2a502af..c7cae08 100755
--- a/mbtk/test/audio_test.c
+++ b/mbtk/test/audio_test.c
@@ -116,7 +116,9 @@
"\t 0 Open PCM\n"
"\t 1 Play Stream\n"
"\t 2 Play file\n"
- "\t 3 Close\n"
+ "\t 3 play mp3\n"
+ "\t 4 Close Player\n"
+ "\t 5 exit\n"
"\t others exit\n\n"
"operator >> ");
@@ -143,7 +145,7 @@
memset(databuf, 0, sizeof(databuf));
while(0 < (size = read(fd, databuf, sizeof(databuf))))
{
- Ql_Rxgain_Set(2);
+ // Ql_Rxgain_Set(2);
if(-1 == Ql_AudPlayer_Play(play_hdl, databuf, size))
break;
}
@@ -172,14 +174,16 @@
close(fd);
break;
case 3:
+ // aplay_thread(MBTK_AUD_DEMO_WAV);
+ Ql_Rxgain_Set(11);
+ Ql_Mp3_To_Play("/data/mp3demo.mp3", play_hdl, 0);
+ break;
+ case 4:
if(0 == play_hdl)
continue;
Ql_AudPlayer_Close(play_hdl);
break;
- case 4:
- // aplay_thread(MBTK_AUD_DEMO_WAV);
- Ql_Mp3_To_Play("/data/mp3demo.mp3", play_hdl, 0);
- break;
+ case 5:
default:
return;
}
@@ -202,11 +206,11 @@
printf("=======arec======\n"
"\t 0 Open PCM\n"
"\t 1 Start Record\n"
- "\t 2 Get state\n"
- "\t 3 Pause\n"
- "\t 4 Resume\n"
- "\t 5 Stop\n"
- "\t 6 Close\n"
+ // "\t 2 Get state\n"
+ // "\t 3 Pause\n"
+ // "\t 4 Resume\n"
+ // "\t 5 Stop\n"
+ "\t 2 Stop/Close Recorder\n"
"\t others exit\n\n"
"operator >> ");
@@ -254,15 +258,6 @@
}
break;
case 2:
- // printf("arec state : %d\n", state);
- break;
- case 3:
- break;
- case 4:
- break;
- case 5:
- break;
- case 6:
Ql_AudRecorder_Close();
if(record_fd > 0)
{
@@ -270,6 +265,14 @@
record_fd = 0;
}
break;
+ case 3:
+ // break;
+ case 4:
+ // break;
+ case 5:
+ // break;
+ case 6:
+ // break;
default:
return;
}
@@ -289,14 +292,14 @@
{
printf("=========audio main=========\n"
"\t0 exit\n"
- "\t1 aplay\n"
- "\t2 arec\n"
- "\t3 set speaker Volume\n"
- "\t4 get speaker Volume\n"
- "\t5 set mic Volume\n"
- "\t6 get mic Volume\n"
- "\t7 tts\n"
- "\t8 tone\n"
+ "\t1 Ql_aplay\n"
+ "\t2 Ql_arec\n"
+ "\t3 audio record\n"
+ "\t4 player stream\n"
+ // "\t5 set mic Volume\n"
+ // "\t6 get mic Volume\n"
+ // "\t7 tts\n"
+ // "\t8 tone\n"
"operator: >> ");
fgets(operator, sizeof(operator), stdin);