錄音加vad

#include#include#include#include#include//#include "stdio.h"#include "wb_vad.h"#include#includeusing namespace std;// 用于存儲語言塊的信息class BlockInfo{? ? public:? ? ? ? bool valid;// 是否有效語音? ? ? ? char* p;// 語音塊的首地址? ? ? ? int len;// 語言塊的大小? ? public:? ? ? ? BlockInfo()? ? ? ? {? ? ? ? ? ? valid = false;? ? ? ? ? ? p = NULL;? ? ? ? ? ? len = 0;? ? ? ? }? ? ? ? ~BlockInfo()? ? ? ? {? ? ? ? ? ? if(NULL == p) return;? ? ? ? ? ? delete p;? ? ? ? }? ? ? ? void set(char* ptr, int length)? ? ? ? {? ? ? ? ? ? p = ptr;? ? ? ? ? ? valid = true;? ? ? ? ? ? len = length;? ? ? ? }};queueglobal_full;queueglobal_valid;int flag = 0; // 當(dāng)前錄音是停止的int irstart = 0;// 判斷是否重啟char filename[1024] = "0";void? vad(char* ptr, int size){? ? int valitSize = 0;// 有效數(shù)據(jù)長度? ? int totalCount = size/512 + (size % 512) > 0 ? 1 : 0;// 總數(shù)據(jù)塊 數(shù)量? ? int useTotalCount = 1;// 使用過的數(shù)據(jù)塊 數(shù)量? ? int i, temp;? ? float indata[FRAME_LEN];? ? VadVars *vadstate;? ? wb_vad_init(&(vadstate));//vad初始化? ? while (useTotalCount <= totalCount)? ? {? ? ? ? int pos = 0;// 被處理的數(shù)據(jù)塊的使用偏移量? ? ? ? char *p = ptr + (useTotalCount -1) * 512;// 被處理數(shù)據(jù)塊在ptr 中的首地址? ? ? ? int cursize = (useTotalCount != totalCount) ? 512 : (size - (useTotalCount-1)*512);// 被處理的數(shù)據(jù)塊的大小? ? ? ? ++useTotalCount;? ? ? ? // 獲取并轉(zhuǎn)換成vad 待處理數(shù)據(jù)? ? ? ? for (i = 0; i65535 / 2)? ? ? ? ? ? ? ? indata[i] = indata[i] - 65536;? ? ? ? }// 存儲語音塊的信息到 global_full? ? ? ? BlockInfo* bi = new BlockInfo();? ? ? ? if (1 == wb_vad(vadstate, indata))//進(jìn)行vad檢測? ? ? ? {? ? ? ? ? ? char* buf = new char[pos];? ? ? ? ? ? memcpy(buf, p, pos);? ? ? ? ? ? bi->set(buf, pos);? ? ? ? ? ? valitSize += cursize;? ? ? ? }? ? ? ? global_full.push(bi);? ? }? ? wb_vad_exit(&(vadstate));? ? fcloseall();}// 刪除有效語音塊中的數(shù)據(jù)(刪除所有、刪除3s)void clean_queue(queue* q, bool cleanall){? ? if(NULL == q) return ;? ? BlockInfo* bi = NULL;? ? if(cleanall)? ? {? ? ? ? while(q->size() > 0)? ? ? ? {? ? ? ? ? ? bi = q->front();? ? ? ? ? ? q->pop();? ? ? ? ? ? delete bi;? ? ? ? }? ? }? ? else? ? {? ? ? ? int count = 62*3;? ? ? ? while(q->size() > 0 && count > 0)? ? ? ? {? ? ? ? ? ? bi = q->front();? ? ? ? ? ? q->pop();? ? ? ? ? ? delete bi;? ? ? ? ? ? --count;? ? ? ? }? ? }}// 檢查有效語音塊集合的狀態(tài)// 0:save, 1: clean all, 2:clean by 3s, 3:ingoreint check_save(bool flag){? ? int size = global_valid.size();? ? if(flag)? ? {? ? ? ? return(size > 62*3*2) ? 2 : 3;? ? }? ? else? ? {? ? ? ? return (size < 62 || size > 62*3) ? 1 : 0;? ? }}char* get_time(){? ? struct tm *newtime;? ? char* outfile = new char[128]{0};? ? time_t t1 = time(NULL);? ? newtime = localtime(&t1);? ? strftime(outfile,128,"data_%Y%m%d_%H%M%S.wav",newtime);? ? return outfile;}// 存儲語音塊集合中的數(shù)據(jù)到文件void save_queue(queue* q)

{

if(NULL == q) return;

char* savename = get_time();

int fd = open(savename, O_CREAT|O_WRONLY|O_TRUNC, 0777);

if(fd < 0)

{

printf("Fail to open or create file!\n");

exit(1);

}

BlockInfo* bi = NULL;

while(global_valid.size() > 0)

{

bi = global_valid.front();

global_valid.pop();

write(fd, bi->p, bi->len);

delete bi;

}

delete savename;

close(fd);

}

// 檢查語音塊集合

void check_queue()

{

if(0 ==global_full.size()) return;

BlockInfo* bi = NULL;

int check_save_ret = 0;

// 對每一塊語音進(jìn)行檢查

while(global_full.size() > 0)

{

bi =? global_full.front();

global_full.pop();

printf("vaild count %d\n", global_valid.size());

// 判斷有效語音集合的狀態(tài)

if(false == bi->valid)

{

delete bi;

check_save_ret = check_save(false);

// 0:save, 1: clean all, 2:clean by 3s, 3:ingore

}

else

{

global_valid.push(bi);

// 0:save, 1: clean all, 2:clean by 3s, 3:ingore

check_save_ret = check_save(true);

}

// 針對有效語音塊集合的狀態(tài) 做對應(yīng)動作( 0:save, 1: clean all, 2:clean by 3s, 3:ingore)

switch(check_save_ret)

{

// 0:save, 1: clean all, 2:clean by 3s, 3:ingore

case 0:

save_queue(&global_valid);

break;

case 1:

clean_queue(&global_valid, true);

break;

case 2:

clean_queue(&global_valid, false);

break;

case 3:

break;

default:

printf("logic err\n");

break;

}

}

}

void start()

{

printf("準(zhǔn)備錄音...\n");

flag = 1;

long loops;

int rc;

int size;

snd_pcm_t *handle;

snd_pcm_hw_params_t *params;

unsigned int val;

int dir;

snd_pcm_uframes_t frames;

char *buffer;

rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE, 0);

if (rc < 0)

{

fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc));

exit(1);

}

/* Allocate a hardware parameters object. */

snd_pcm_hw_params_alloca(?ms);

/* Fill it in with default values. */

if((rc=snd_pcm_hw_params_any(handle, params))<0)

{

fprintf(stderr,"cannot initialize hd params (%s)\n",snd_strerror(rc));

exit(1);

}

/* Set the desired hardware parameters. */

/* Interleaved mode */

if((rc=snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED))<0)

{

fprintf(stderr,"cannot set access type (%s)\n",snd_strerror(rc));

exit(1);

}

/* Signed 16-bit little-endian format */

if((rc=snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE))<0)

{

fprintf(stderr,"cannot set sample format (%s)\n",snd_strerror(rc));

exit(1);

}

/* One channels (stereo) */

if((rc=snd_pcm_hw_params_set_channels(handle, params, 1))<0)

{

fprintf(stderr,"cannot set channels (%s)\n",snd_strerror(rc));

exit(1);

}

/* 16000 bits/second sampling rate (CD quality) */

val = 16000;

if((rc=snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir))<0)

{

fprintf(stderr,"cannot set sample rate_near (%s)\n",snd_strerror(rc));

exit(1);

}

/* Set period size to 32 frames. */

frames = 32;

if((rc=snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir))<0)

{

fprintf(stderr,"cannot set perios_size_near (%s)\n",snd_strerror(rc));

exit(1);

}

/* Write the parameters to the driver */

rc = snd_pcm_hw_params(handle, params);

if (rc < 0)

{

fprintf(stderr,

"unable to set hw parameters: %s\n",

snd_strerror(rc));

exit(1);

}

/* Use a buffer large enough to hold one period */

if((rc=snd_pcm_hw_params_get_period_size(params, &frames, &dir))<0)

{

fprintf(stderr,"cannot get period_size (%s)\n",snd_strerror(rc));

exit(1);

}

size = frames*8; /* 2 bytes/sample, 1 channels */

buffer = (char *) malloc(size);

printf("%d\n",frames);

printf("%d\n",size);

while (flag)

{

rc = snd_pcm_readi(handle, buffer, frames*4);

if (rc == -EPIPE)

{

/* EPIPE means overrun */

fprintf(stderr, "overrun occurred\n");

snd_pcm_prepare(handle);

}

else if (rc < 0)

{

fprintf(stderr, "error from read: %s\n", snd_strerror(rc));

}

else if (rc != (int)frames*4)

{

fprintf(stderr, "short read, read %d frames\n", rc);

}

vad(buffer,size);

// 檢查語音塊集合

check_queue();

}

if((rc=snd_pcm_drain(handle))<0)

{

fprintf(stderr,"cannot drain (%s)\n",snd_strerror(rc));

exit(1);

}

if((snd_pcm_close(handle))<0)

{

fprintf(stderr,"cannot close (%s)\n",snd_strerror(rc));

exit(1);

}

free(buffer);

// 錄音結(jié)束

printf("錄音完成提澎,準(zhǔn)備寫入文件\n");

if(irstart)

{

start();

}

}

void _savename(const char* savename)

{

strncpy(filename, savename, strlen(savename));

}

void stop()

{

// 讓錄音停止

flag = 0;

irstart = 0;

}

void rstart(const char* savename)

{

_savename(savename);

stop();

irstart = 1;

}

void* thread_cb(void* savename)

{

_savename((char*)savename);

start();

return NULL;

}

void start_with_thread(const char* savename)

{

pthread_t pid;

pthread_create(&pid, NULL, thread_cb, (void*)savename);

pthread_detach(pid);

}

int main(void)

{

int test = 0;

// 1 錄音

// 2 停止錄音

// 3 重啟

while(1)

{

scanf("%d", &test);

if(1 == test)

{

start_with_thread("start.wav");

}

else if(2 == test)

{

stop();

//savefile();

}

else if(3 == test)

{

rstart("restartname.wav");

}

else

{

break;

}

}

sleep(1);

printf("程序退出\n");

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市替饿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌改橘,老刑警劉巖绵脯,帶你破解...
    沈念sama閱讀 222,865評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異慰照,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)琉朽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,296評論 3 399
  • 文/潘曉璐 我一進(jìn)店門毒租,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人箱叁,你說我怎么就攤上這事墅垮。” “怎么了耕漱?”我有些...
    開封第一講書人閱讀 169,631評論 0 364
  • 文/不壞的土叔 我叫張陵算色,是天一觀的道長。 經(jīng)常有香客問我螟够,道長灾梦,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,199評論 1 300
  • 正文 為了忘掉前任妓笙,我火速辦了婚禮若河,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘寞宫。我一直安慰自己萧福,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,196評論 6 398
  • 文/花漫 我一把揭開白布辈赋。 她就那樣靜靜地躺著鲫忍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪钥屈。 梳的紋絲不亂的頭發(fā)上悟民,一...
    開封第一講書人閱讀 52,793評論 1 314
  • 那天,我揣著相機(jī)與錄音焕蹄,去河邊找鬼。 笑死阀溶,一個胖子當(dāng)著我的面吹牛腻脏,可吹牛的內(nèi)容都是我干的鸦泳。 我是一名探鬼主播,決...
    沈念sama閱讀 41,221評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼永品,長吁一口氣:“原來是場噩夢啊……” “哼做鹰!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起鼎姐,我...
    開封第一講書人閱讀 40,174評論 0 277
  • 序言:老撾萬榮一對情侶失蹤钾麸,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后炕桨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體饭尝,經(jīng)...
    沈念sama閱讀 46,699評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,770評論 3 343
  • 正文 我和宋清朗相戀三年献宫,在試婚紗的時候發(fā)現(xiàn)自己被綠了钥平。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,918評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡姊途,死狀恐怖涉瘾,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情捷兰,我是刑警寧澤立叛,帶...
    沈念sama閱讀 36,573評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站贡茅,受9級特大地震影響秘蛇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜友扰,卻給世界環(huán)境...
    茶點故事閱讀 42,255評論 3 336
  • 文/蒙蒙 一彤叉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧村怪,春花似錦秽浇、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,749評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至梭域,卻和暖如春斑举,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背病涨。 一陣腳步聲響...
    開封第一講書人閱讀 33,862評論 1 274
  • 我被黑心中介騙來泰國打工富玷, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 49,364評論 3 379
  • 正文 我出身青樓赎懦,卻偏偏與公主長得像雀鹃,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子励两,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,926評論 2 361

推薦閱讀更多精彩內(nèi)容

  • 一.聲音參數(shù)基本概念: 聲音是連續(xù)模擬量黎茎,計算機(jī)將它離散化之后用數(shù)字表示,就有了以下幾個名詞術(shù)語当悔。 樣本長度(sa...
    cs1001閱讀 2,753評論 0 2
  • CUDA從入門到精通(零):寫在前面 本文原版鏈接: 在老板的要求下傅瞻,本博主從2012年上高性能計算課程開始接觸C...
    Pitfalls閱讀 3,621評論 1 3
  • 一.聲音參數(shù)基本概念: 聲音是連續(xù)模擬量,計算機(jī)將它離散化之后用數(shù)字表示盲憎,就有了以下幾個名詞術(shù)語嗅骄。 樣本長度(sa...
    cs1001閱讀 5,418評論 0 3
  • 教程一:視頻截圖(Tutorial 01: Making Screencaps) 首先我們需要了解視頻文件的一些基...
    90后的思維閱讀 4,713評論 0 3
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,749評論 0 33