放出声波生成器源代码

fireseed 2003-08-28 11:36:26
加精
//WaveFunc.h
#include <Windows.h>

typedef struct tagWAVEINFO
{
WORD cbSize;
DWORD dwSamplePerSec;
double dSeconds;
WORD wChannels;
WORD wBitsPerSample;
DWORD dwHz;
BYTE byVolume;
DWORD dwMode;
} WAVEINFO, *PWAVEINFO;

void* CreateWaveData( const PWAVEINFO pwi, LPWAVEFORMATEX pwf, DWORD *pBufLen );
BOOL WriteWaveData( LPWAVEFORMATEX pwf, void *pBuf, DWORD dwBufLen );

...全文
214 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
imaboy_a 2010-05-27
编译没通过!!
  • 打赏
  • 举报
回复
antijpn 2003-09-02
这个东西还真没研究过,不过楼主的这个应该不支持ASIO吧?
  • 打赏
  • 举报
回复
fireseed 2003-08-28

WAVEINFO wi;
wi.cbSize = sizeof(wi); // 大小
wi.byVolume = 200; // 音量,最大255
wi.dSeconds = 2.0; // 时间长度
wi.dwHz = 1000; // 频率
wi.dwSamplePerSec = 44100; // 采样率
wi.wBitsPerSample = 16; // 位率
wi.wChannels = 2; // 立体声、单声道

WAVEFORMATEX wf;
DWORD dwBufLen;
void *pBuf = CreateWaveData( &wi, &wf, &dwBufLen );
WriteWaveData( &wf, pBuf, dwBufLen );
  • 打赏
  • 举报
回复
fireseed 2003-08-28

WAVEINFO wi;
wi.cbSize = sizeof(wi); // 大小
wi.byVolume = 200; // 音量,最大255
wi.dSeconds = 2.0; // 时间长度
wi.dwHz = 1000; // 频率
wi.dwSamplePerSec = 44100; // 采样率
wi.wBitsPerSample = 16; // 位率
wi.wChannels = 2; // 立体声、单声道

WAVEFORMATEX wf;
DWORD dwBufLen;
void *pBuf = CreateWaveData( &wi, &wf, &dwBufLen );
WriteWaveData( &wf, pBuf, dwBufLen );
  • 打赏
  • 举报
回复
fireseed 2003-08-28
#pragma comment( lib, "Winmm.lib" )

#define _USE_MATH_DEFINES
#include <math.h>
#include "WaveFunc.h"

void CALLBACK waveOutProc( HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2 )
{
if ( uMsg == WOM_DONE )
{
LPWAVEHDR pwh = *(LPWAVEHDR*)(&dwParam1);
waveOutUnprepareHeader( hwo, pwh, sizeof(WAVEHDR) );
delete [](pwh->lpData);
delete pwh;
waveOutClose( hwo );
}
}

void* CreateWaveData( const PWAVEINFO pwi, LPWAVEFORMATEX pwf, DWORD *pBufLen )
{
if ( pwi->cbSize != sizeof(WAVEINFO) )
{
return NULL;
}
double dCurTime;
int nCurValue;
double dCircle = 1.0 / (double)pwi->dwHz; // 周期时间
double dSecPerSample = 1.0 / (double)pwi->dwSamplePerSec; // 采样点距
WORD wBytePerSample = pwi->wBitsPerSample / 8; // 每采样字节数
DWORD dwSampleCount = (DWORD)ceil( pwi->dSeconds * (double)pwi->dwSamplePerSec ); // 全部采样点数
*pBufLen = dwSampleCount * wBytePerSample * pwi->wChannels; // 数据长度
void *pData = new char[*pBufLen];
for ( DWORD i = 0; i < dwSampleCount; i++ )
{
dCurTime = dSecPerSample * (double)i / dCircle;
dCurTime = ( dCurTime - (int)dCurTime ) * dCircle;
nCurValue = (int)(sin( dCurTime * M_PI * 2 / dCircle ) *
pow( 2.0, pwi->wBitsPerSample ) / 2.0 * (double)pwi->byVolume / 255.0);
for ( WORD j = 0; j < pwi->wChannels; j++ )
{
for ( WORD k = 0; k < wBytePerSample; k++ )
{
((char*)pData)[ ( i * pwi->wChannels + j ) * wBytePerSample + k ] =
( nCurValue << ( ( 3 - k ) * 8) ) >> 24;
}
}
}
pwf->cbSize = sizeof(WAVEFORMATEX);
pwf->wFormatTag = WAVE_FORMAT_PCM;
pwf->nChannels = pwi->wChannels;
pwf->nSamplesPerSec = pwi->dwSamplePerSec;
pwf->wBitsPerSample = pwi->wBitsPerSample;
pwf->nBlockAlign = wBytePerSample * pwi->wChannels;
pwf->nAvgBytesPerSec = pwi->dwSamplePerSec * pwf->nBlockAlign;
return pData;
}



BOOL WriteWaveData( LPWAVEFORMATEX pwf, void *pBuf, DWORD dwBufLen )
{
MMRESULT mr;
HWAVEOUT hWaveOut;
WAVEHDR *pWaveHdr = new WAVEHDR;

DWORD dwAbility = 1;
DWORD dwTemp = (DWORD)( log( pwf->nSamplesPerSec / 11025 ) / log(2) );
dwAbility <<= ( ( ( dwTemp * 2 ) + ( 2 - 16 / pwf->wBitsPerSample ) ) * 2 + ( pwf->nChannels - 1 ) );

WAVEOUTCAPS woc;
waveOutGetDevCaps( WAVE_MAPPER, &woc, sizeof(woc) );
if ( 0 == ( dwAbility & woc.dwFormats ) ||
0 != (pwf->nSamplesPerSec % 11025) ||
( 2 - 16 / pwf->wBitsPerSample ) > 1 ||
( pwf->nChannels - 1 ) > 1 )
{
return FALSE;
}
pwf->nBlockAlign = pwf->nChannels * pwf->wBitsPerSample / 8;
pwf->nAvgBytesPerSec = pwf->nBlockAlign * pwf->nSamplesPerSec;

mr = waveOutOpen( &hWaveOut, WAVE_MAPPER, pwf,
(DWORD_PTR)waveOutProc, 0, CALLBACK_FUNCTION );
if ( mr )
{
return FALSE;
}
ZeroMemory( pWaveHdr, sizeof(WAVEHDR) );
pWaveHdr->lpData = (LPSTR)pBuf;
pWaveHdr->dwBufferLength = dwBufLen;
mr = waveOutPrepareHeader( hWaveOut,pWaveHdr, sizeof(WAVEHDR) );
mr = waveOutWrite( hWaveOut, pWaveHdr, sizeof(WAVEHDR) );
return TRUE;
}
  • 打赏
  • 举报
回复
相关推荐
发帖
C语言
加入

6.6w+

社区成员

C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
申请成为版主
帖子事件
创建了帖子
2003-08-28 11:36
社区公告
暂无公告