分享
 
 
 

Pedal Point源码发布及说明(6)

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

本文属spanzhang原创,其blog地址为:http://blog.csdn.net/spanzhang。引用或转贴请注明出处,谢谢!!

/*/////////////////////////////////////////////////////////////////////

文件:waveRecorder.cpp

描述:录音类实现文件

作者:张友邦

时间:2004-09-10

声明:本文件系作者辛苦熬夜的产物,任何人使用本文件请保留本描述文本。

历史:

/*/////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////

#include "stdAfx.h"

#include "waveRecorder.h"

#include "graphSeed.h"

//////////////////////////////////////////////////////////////////////

// Constructions/Destruction

namespace wa

{

//////////////////////////////////////////////////////////////////////

#define MAXINPUTBUFFERS 25

#define BUFFERSIZE 1024 * 1

float waveRecorder::sampleCoef = 1.0;

waveRecorder::waveRecorder()

{

memset(&waveFormat, 0x00, sizeof(WAVEFORMATEX));

waveFormat.wFormatTag = WAVE_FORMAT_PCM;

waveFormat.nChannels = 1;

waveFormat.wBitsPerSample = 8;

waveFormat.cbSize = 0;

waveFormat.nSamplesPerSec = 44100;

waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec

*(waveFormat.wBitsPerSample>>3);

waveFormat.nBlockAlign =

(waveFormat.wBitsPerSample>>3)*

waveFormat.nChannels;

status = E_STATUS_STOPED;

sampleCoef = 1.0;

}

waveRecorder::waveRecorder(const WAVEFORMATEX& waveFormatConfig)

{

waveFormat = waveFormatConfig;

status = E_STATUS_STOPED;

sampleCoef = 1.0;

}

waveRecorder::waveRecorder(const int& frequency, const int& channels)

{

memset(&waveFormat, 0x00, sizeof(WAVEFORMATEX));

waveFormat.wFormatTag = WAVE_FORMAT_PCM;

waveFormat.nChannels = channels;

waveFormat.wBitsPerSample = 16;

waveFormat.cbSize = 0;

waveFormat.nSamplesPerSec = frequency;

waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec

*(waveFormat.wBitsPerSample>>3);

waveFormat.nBlockAlign =

(waveFormat.wBitsPerSample>>3)*

waveFormat.nChannels;

status = E_STATUS_STOPED;

sampleCoef = 1.0;

}

waveRecorder::~waveRecorder()

{

try

{

stop();

}

catch (...)

{

}

}

float waveRecorder::constraint(char v)

{

if (v == -128)

return 127.0 * sampleCoef;

return float(v) * sampleCoef;

}

void waveRecorder::smooth(char* input, int* output, int length,

double smoothness/* = 0.8*/, int scale /*= 100*/)

{

double a = 1.0 - (2.4 / scale);

double b = smoothness;

double acoef = a;

double bcoef = a * b;

double ccoef = a * b * b;

double mastergain = 1.0 / (-1.0 / (log(a) + 2.0 * log(b)) +

2.0 / (log(a) + log(b)) - 1.0 / log(a));

double again = mastergain;

double bgain = mastergain * (log(a * b * b) * (log(a) - log(a * b)) /

((log(a * b * b) - log(a * b)) * log(a * b))

- log(a) / log(a * b));

double cgain = mastergain * (-(log(a) - log(a * b)) /

(log(a * b * b) - log(a * b)));

double areg = 0;

double breg = 0;

double creg = 0;

//第一次循环,取得reg的平均值

for (int j = 0; j < length; ++j)

{

float v = constraint(input[j]);

areg = acoef * areg + v;

breg = bcoef * breg + v;

creg = ccoef * creg + v;

}

//得到基音

float base = again * areg + bgain * breg + cgain * creg;

output[0] = base;

//用基音作为其始作循环得到其他数据

for (int i = 1; i < length; ++i)

{

int v = constraint(input[i - 1]);

areg = acoef * areg + v;

breg = bcoef * breg + v;

creg = ccoef * creg + v;

output[i] = again * areg + bgain * breg + cgain * creg - base;

}

}

void CALLBACK waveRecorder::waveInProc

(

HWAVEIN hwi, //音频设备句柄

UINT uMsg, //消息标识

DWORD dwInstance, //用户定义数据

DWORD dwParam1, //消息参数

DWORD dwParam2 //消息参数

)

{

//过滤消息(只处理数据消息)

if (uMsg != WIM_DATA)

return;

//取得参数值

waveRecorder* me = (waveRecorder*)(dwInstance);

LPWAVEHDR lpHdr = (LPWAVEHDR)(dwParam1);

//对数据作低通滤波

if (lpHdr->dwBufferLength >= WAVE_BUFFER_LENGTH &&

::WaitForSingleObject(pb::eventMutex, INFINITE) == WAIT_OBJECT_0)

{

smooth(lpHdr->lpData, gs::graphSeed::lowpassWave, WAVE_BUFFER_LENGTH, 0.9999, 100);

::ReleaseMutex(pb::eventMutex);

}

//准备新缓冲

if (me->status == E_STATUS_STARTED)

{

HWAVEIN& hRecord = me->hRecord;

::waveInUnprepareHeader(hRecord, lpHdr, sizeof(WAVEHDR));

::waveInPrepareHeader(hRecord, lpHdr, sizeof(WAVEHDR));

::waveInAddBuffer(hRecord, lpHdr, sizeof(WAVEHDR));

}

else

{

safeDelete(lpHdr->lpData);

safeDelete(lpHdr);

}

}

//开始

void waveRecorder::start() throw (std::exception)

{

if(status == E_STATUS_STARTED)

return;

//打开音频设备

MMRESULT mmReturn = ::waveInOpen

(

&hRecord,

WAVE_MAPPER,

&waveFormat,

(DWORD)(waveRecorder::waveInProc),

(DWORD)(this),

CALLBACK_FUNCTION

);

switch(mmReturn)

{

case MMSYSERR_NOERROR: //成功

break;

case MMSYSERR_ALLOCATED:

throw std::exception("Specified resource is already allocated.");

case MMSYSERR_BADDEVICEID:

throw std::exception("Specified device identifier is out of range.");

case MMSYSERR_NODRIVER:

throw std::exception("No device driver is present.");

case MMSYSERR_NOMEM:

throw std::exception("Unable to allocate or lock memory.");

case WAVERR_BADFORMAT:

throw std::exception("Attempted to open with an unsupported waveform-audio format.");

default:

throw std::exception("Unknown error occured.");

}

//加入缓冲

for (int i = 0; i < MAXINPUTBUFFERS; ++i)

{

LPWAVEHDR lpHdr = new WAVEHDR;

memset(lpHdr, 0x00, sizeof(WAVEHDR));

lpHdr->lpData = new char[BUFFERSIZE];

lpHdr->dwBufferLength = BUFFERSIZE;

MMRESULT mmReturn = ::waveInPrepareHeader(hRecord, lpHdr, sizeof(WAVEHDR));

if (mmReturn != MMSYSERR_NOERROR) //Memory leaks

throw std::exception("waveInPrepareHeader error in startRedording.");

mmReturn = ::waveInAddBuffer(hRecord, lpHdr, sizeof(WAVEHDR));

if (mmReturn != MMSYSERR_NOERROR) //Memory leaks

throw std::exception("waveInAddBuffer error in startRedording.");

}

//开始录音

mmReturn = ::waveInStart(hRecord);

if (mmReturn != MMSYSERR_NOERROR)

{

throw std::exception("waveInStart error.");

}

status = E_STATUS_STARTED;

}

//停止录音

void waveRecorder::stop() throw (std::exception)

{

if(status != E_STATUS_STARTED)

return;

//停止录音

MMRESULT mmReturn = ::waveInStop(hRecord);

switch(mmReturn)

{

case MMSYSERR_NOERROR: //成功

break;

case MMSYSERR_INVALHANDLE:

throw std::exception("Specified device handle is invalid.");

break;

case MMSYSERR_NODRIVER:

throw std::exception("No device driver is present.");

break;

case MMSYSERR_NOMEM:

throw std::exception("Unable to allocate or lock memory.");

break;

default:

throw std::exception("Unknown error occured.");

}

status = E_STATUS_STOPED;

//重置设备

//DEL_CODE

//(<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

mmReturn = ::waveInReset(hRecord);

switch(mmReturn)

{

case MMSYSERR_NOERROR: //成功

break;

case MMSYSERR_INVALHANDLE:

throw std::exception("Specified device handle is invalid.");

case MMSYSERR_NODRIVER:

throw std::exception("No device driver is present.");

case MMSYSERR_NOMEM:

throw std::exception("Unable to allocate or lock memory.");

default:

throw std::exception("Unknown error occured.");

}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>)

//关闭设备

mmReturn = ::waveInClose(hRecord);

switch(mmReturn)

{

case MMSYSERR_NOERROR: //成功

break;

case MMSYSERR_INVALHANDLE:

throw std::exception("Specified device handle is invalid.");

break;

case MMSYSERR_NODRIVER:

throw std::exception("No device driver is present.");

break;

case MMSYSERR_NOMEM:

throw std::exception("Unable to allocate or lock memory.");

break;

case WAVERR_STILLPLAYING:

throw std::exception("There are still buffers in the queue.");

break;

default:

throw std::exception("Unknown error occured.");

}

status = E_STATUS_STOPED;

}

//////////////////////////////////////////////////////////////////////

}; //namespace wa

//////////////////////////////////////////////////////////////////////

//End of this file.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有