以下是windows 音频接口调用的例子,可以在vc 或minGW下编译,需要借助库文件libwinmm.a
//#include "stdafx.h"
#include <windows.h>
#include <math.h>
#include <mmsystem.h>
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159
#define SAMPLE_RATE 11025
#define OUT_BUFFER_SIZE 4096
static PBYTE pBuffer1, pBuffer2 ;
static PWAVEHDR pWaveHdr1, pWaveHdr2 ;
static WAVEFORMATEX waveformat ;
HWAVEOUT hWaveOut;
void FillBuffer(unsigned char * buffer,int iFreq)
{
static double fAngle ;
int i ;
for (i = 0 ; i < OUT_BUFFER_SIZE ; i++)
{
buffer [i] = (char)(127 + 127 * sin (fAngle)) ;
fAngle += 2 * PI * iFreq / SAMPLE_RATE ;
if ( fAngle > 2 * PI)
fAngle -= 2 * PI ;
}
}
int CALLBACK waveOutProc
(
HWAVEOUT hWaveOut,
UINT uMsg,
DWORD dwInstance,
DWORD dwParam1,
DWORD dwParam2
)
{
static int iFreq=400;
LPWAVEHDR pWaveHeader=(LPWAVEHDR)dwParam1;
switch(uMsg)
{
case MM_WOM_OPEN:
/* FillBuffer (pBuffer1, iFreq) ;
waveOutWrite (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
FillBuffer (pBuffer2, iFreq) ;
waveOutWrite (hWaveOut, pWaveHdr2, sizeof (WAVEHDR)) ;*/
return TRUE ;
case MM_WOM_DONE:
iFreq +=20;
waveOutUnprepareHeader(hWaveOut,pWaveHeader,sizeof(WAVEHDR));
FillBuffer ((unsigned char *)pWaveHeader->lpData, iFreq) ;
waveOutPrepareHeader(hWaveOut,pWaveHeader,sizeof(WAVEHDR));
waveOutWrite (hWaveOut, pWaveHeader, sizeof (WAVEHDR)) ;
return TRUE ;
case MM_WOM_CLOSE:
waveOutUnprepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
waveOutUnprepareHeader (hWaveOut, pWaveHdr2, sizeof (WAVEHDR)) ;
free (pWaveHdr1) ;
free (pWaveHdr2) ;
free (pBuffer1) ;
free (pBuffer2) ;
hWaveOut = NULL ;
return TRUE ;
default:
return TRUE ;
}
}
void main(int argc, char* argv[])
{
pWaveHdr1 = (PWAVEHDR)malloc (sizeof (WAVEHDR)) ;
pWaveHdr2 = (PWAVEHDR)malloc (sizeof (WAVEHDR)) ;
pBuffer1 = (PBYTE)malloc (OUT_BUFFER_SIZE) ;
pBuffer2 = (PBYTE)malloc (OUT_BUFFER_SIZE) ;
if (!pWaveHdr1 || !pWaveHdr2 || !pBuffer1 || !pBuffer2)
{
if (!pWaveHdr1) free (pWaveHdr1) ;
if (!pWaveHdr2) free (pWaveHdr2) ;
if (!pBuffer1) free (pBuffer1) ;
if (!pBuffer2) free (pBuffer2) ;
}
waveformat.wFormatTag = WAVE_FORMAT_PCM ;
waveformat.nChannels = 1 ;
waveformat.nSamplesPerSec = SAMPLE_RATE ;
waveformat.nAvgBytesPerSec = SAMPLE_RATE ;
waveformat.nBlockAlign = 1 ;
waveformat.wBitsPerSample = 8 ;
waveformat.cbSize = 0 ;
if (waveOutOpen (&hWaveOut, WAVE_MAPPER, &waveformat,
(DWORD)waveOutProc, 0, CALLBACK_FUNCTION)!= MMSYSERR_NOERROR)
{
free (pWaveHdr1) ;
free (pWaveHdr2) ;
free (pBuffer1) ;
free (pBuffer2) ;
}
// Set up headers and prepare them
pWaveHdr1->lpData = (char *)pBuffer1 ;
pWaveHdr1->dwBufferLength = OUT_BUFFER_SIZE ;
pWaveHdr1->dwBytesRecorded = 0 ;
pWaveHdr1->dwUser = 0 ;
pWaveHdr1->dwFlags = 0 ;
pWaveHdr1->dwLoops = 1 ;
pWaveHdr1->lpNext = pWaveHdr2;
pWaveHdr1->reserved = 0 ;
waveOutPrepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
pWaveHdr2->lpData = (char *)pBuffer2 ;
pWaveHdr2->dwBufferLength = OUT_BUFFER_SIZE ;
pWaveHdr2->dwBytesRecorded = 0 ;
pWaveHdr2->dwUser = 0 ;
pWaveHdr2->dwFlags = 0 ;
pWaveHdr2->dwLoops = 1 ;
pWaveHdr2->lpNext = pWaveHdr1;
pWaveHdr2->reserved = 0 ;
waveOutPrepareHeader (hWaveOut, pWaveHdr2, sizeof (WAVEHDR)) ;
FillBuffer (pBuffer1, 2000) ;
waveOutWrite (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
FillBuffer (pBuffer2, 2000) ;
waveOutWrite (hWaveOut, pWaveHdr2, sizeof (WAVEHDR)) ;
getchar();
waveOutClose(hWaveOut);
}