📄 snd_rec.c
字号:
/******************************* (C) Embest ***********************************
* File Name : snd_rec.c
* Author : tary
* Date : 2009-06-08
* Version : 0.1
* Description : 录音生成WAV文件
******************************************************************************/
/* Includes -----------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "ucos_ii.h"
#include "ff.h"
#include "drv_init.h"
#include "mp3_dbg.h"
#define LOCAL_DBG 1
/* Variables ----------------------------------------------------------------*/
const data_fmt_t snd_rec_fmt[] = {
{
WAVE_FORMAT_PCM,
CHANNEL_MONO,
SAMPLE_RATE_8000,
(1 * BITS_PER_SAMPLE_8 / 8) * SAMPLE_RATE_8000,
(1 * BITS_PER_SAMPLE_8 / 8),
BITS_PER_SAMPLE_8
},
};
int srec_len = 0;
int srec_data_off = 0;
int srec_file_open(char* filename, int mode) {
char fnambuf[0x80];
int r;
sprintf(fnambuf, "1:\\wavefile\\%s", filename);
r = f_open(&fil, fnambuf, FA_WRITE | FA_CREATE_NEW);
if (FR_OK != r) {
printf("\r\n err %d", r);
return -2;
}
return 0;
}
int srec_fill_hdr(char* binfile, int* fmt_of, int* data_of) {
riff_chunk_t* prc;
int bof = 0;
int fact = 0;
// int br;
prc = (riff_chunk_t*)&binfile[bof];
memcpy(&prc->id, "RIFF", 4);
memcpy(&prc->data, "WAVE", 4);
prc->size = 0;
bof += 12;
prc = (riff_chunk_t*)&binfile[bof];
if (fmt_of != NULL) {
*fmt_of = (char*)prc->data - binfile;
}
memcpy(&prc->id, "fmt ", 4);
memcpy(&prc->data, &snd_rec_fmt[0], sizeof(snd_rec_fmt[0]));
prc->size = 0x12;
if (prc->size != 0x10) {
fact = 1;
}
if (fact) {
//get fact thunk
bof += 8 + prc->size;
prc = (riff_chunk_t*)&binfile[bof];
memcpy(&prc->id, "fact", 4);
memcpy(&prc->data, "star", 4);
prc->size = 4;
}
//get data chunk
bof += 8 + prc->size;
prc = (riff_chunk_t*)&binfile[bof];
memcpy(&prc->id, "data", 4);
if (data_of != NULL) {
*data_of = (char*)prc->data - binfile;
}
#if 0
f_write(&fil, binfile, *data_of, (UINT*)&br);
f_sync(&fil);
f_lseek(&fil, 0);
#endif
return 0;
}
// filebuf_rdy[x]
// == 0 数据缓冲区满
// != 0 数据缓冲区空(可以存数据)
int srec_end_inform(char* buf, int idx, int cnt) {
INT8U r;
filebuf_rdy[filebuf_idx] = 0;
filebuf_idx = !filebuf_idx;
if (filebuf_rdy[filebuf_idx] == 0) {
// stop the play
// DBG_LINE(2, "idx", filebuf_idx);
return -1;
}
// DBG_LINE(0);
adcap_set_buf(filebuf[filebuf_idx], filebuf_rdy[filebuf_idx]);
r = OSSemPost(fr_evt);
if (OS_ERR_NONE != r) {
return 0;
}
return 0;
}
int srec_cont (void) {
// INT8U r;
if (filebuf_rdy[filebuf_idx] == 0) {
filebuf_idx = !filebuf_idx;
if (filebuf_rdy[filebuf_idx] == 0) {
return -1;
}
}
// 打开定时器,继续录音
dcodec_set_buf(filebuf[filebuf_idx], filebuf_rdy[filebuf_idx]);
dcodec_start();
return 0;
}
int srec_doing_svr(void) {
INT8U err;
int idx;
int br, r;
r = 0;
while (0
|| filebuf_rdy[0]
|| filebuf_rdy[1]
) {
OSSemPend(fr_evt, OS_TICKS_PER_SEC / 3, &err);
if (OS_ERR_NONE != err) {
// r = -1;
// break;
r = srec_cont();
if (r < 0) {
break;
}
}
idx = !filebuf_idx;
// 如果无数据
if (filebuf_rdy[idx]) {
// r = -2;
continue;
}
r = f_write(&fil, filebuf[idx], FILEBUF_SIZ, (UINT*)&br);
if (FR_OK != r) {
r = -3;
break;
}
if (br == 0) {
r = -4;
break;
}
// DBG_LINE(2, "idx", idx);
srec_len += FILEBUF_SIZ;
filebuf_rdy[idx] = FILEBUF_SIZ;
r = key_accept();
if (r < 0) {
continue;
}
if (r == KEY_SEL) {
break;
}
}
return r;
}
int srec_start(int fmt_of, int data_of) {
filebuf_rdy[filebuf_idx] = FILEBUF_SIZ - data_of;
filebuf_rdy[!filebuf_idx] = FILEBUF_SIZ;
srec_len = 0;
// adcap_set_fmt((data_fmt_t*)&filebuf[filebuf_idx][fmt_of]);
adcap_set_buf(&filebuf[filebuf_idx][data_of], filebuf_rdy[filebuf_idx]);
adcap_set_inform(srec_end_inform);
fr_evt = OSSemCreate(0);
if (fr_evt == NULL) {
return -1;
}
adcap_start();
return 0;
}
int srec_exit(int err_code) {
INT8U err;
int i, r, br;
for (i = 0; i < FILEBUF_CNT; i++) {
filebuf_rdy[i] = 0;
}
r = f_write(&fil, filebuf[filebuf_idx], adcap_get_pos(), (UINT*)&br);
if (FR_OK != r) {
br = 0;
}
srec_len += br;
f_sync(&fil);
i = srec_len - 8;
f_lseek(&fil, 4);
f_write(&fil, &i, sizeof srec_len, (UINT*)&br);
i = srec_len - srec_data_off;
f_lseek(&fil, srec_data_off - 4);
f_write(&fil, &i, sizeof srec_len, (UINT*)&br);
f_close(&fil);
if (fr_evt != NULL) {
OSSemDel(fr_evt, OS_DEL_ALWAYS, &err);
fr_evt = NULL;
}
printf(" err %d", err_code);
return r;
}
/*=============================================================================
* Function :
* Description :
* Input Para :
* Output Para :
* Return Value :
=============================================================================*/
void snd_rec(void* parg) {
int fmt_of;
char* filename;
int r;
// gets(filename);
filename = ((char**)parg)[1];
if (filename == NULL) {
printf("\r\nargument");
srec_exit(-1);
return;
}
r = srec_file_open(filename, 0);
if (r < 0) {
printf("\r\nfile open");
srec_exit(r);
return;
}
filebuf_idx = 0;
r = srec_fill_hdr(filebuf[filebuf_idx], &fmt_of, &srec_data_off);
if (r < 0) {
printf("\r\nfile fill hdr");
srec_exit(r);
return;
}
r = srec_start(fmt_of, srec_data_off);
if (r < 0) {
printf("\r\nfile start");
srec_exit(r);
return;
}
r = srec_doing_svr();
printf("\r\nsound record svr");
srec_exit(r);
// printf("\r\nthe task snd_rec() is running!");
return;
}
/************************************END OF FILE******************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -