這個程式錄音之後,用PlaySound((LPCSTR)WaveData,0,SND_MEMORY);
照理說會出聲,不知那裏寫錯.請高手指教.
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Button1->Enabled = false;
Button2->Enabled = false;
// Could have used low-level audio functions to play wave file
// but PlaySound is much easier.
//PlaySound("test.wav", 0, SND_FILENAME);
PlaySound((LPCSTR)WaveData,0,SND_MEMORY);
// PlaySound((LPCSTR)&WaveData,NULL,SND_MEMORY);
Button1->Enabled = true;
Button2->Enabled = true; } //--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
} //---------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
// Just in case.
if (WaveData)
delete[] WaveData;
}
//---------------------------------------------
void TForm1::OnWaveMessage(TMessage& msg)
{
// Record buffer is full so free the memory allocated for the
// buffer. After that write out the file.
if (msg.Msg == MM_WIM_DATA) {
waveInClose(WaveHandle);
SaveWaveFile();
WaveHeader.lpData = 0;
if (WaveData) {
delete[] WaveData;
WaveData = 0;
}
Button1->Enabled = true;
Button2->Enabled = false;
Button3->Enabled = true;
}
}
//---------------------------------------------
void TForm1::CheckWaveError(DWORD code)
{
if (code == 0) return;
char buff[256];
// Report a wave out error, if one occurred.
waveInGetErrorText(code, buff, sizeof(buff));
MessageBox(Handle, buff, "Wave Error", MB_OK);
} void TForm1::CheckMMIOError(DWORD code)
{
// Report an mmio error, if one occurred.
if (code == 0) return;
char buff[256];
wsprintf(buff,
"MMIO Error. Error Code: %d", code);
Application->MessageBox(buff, "MMIO Error", 0);
} void TForm1::SaveWaveFile()
{
// Declare the structures we'll need.
MMCKINFO ChunkInfo;
MMCKINFO FormatChunkInfo;
MMCKINFO DataChunkInfo; // Open the file.
HMMIO handle = mmioOpen(
"test.wav", 0, MMIO_CREATE | MMIO_WRITE);
if (!handle) {
MessageBox(0, "Error creating file.", "Error Message", 0);
return;
} // Create RIFF chunk. First zero out ChunkInfo structure.
memset(&ChunkInfo, 0, sizeof(MMCKINFO));
ChunkInfo.fccType = mmioStringToFOURCC("WAVE", 0);
DWORD Res = mmioCreateChunk(
handle, &ChunkInfo, MMIO_CREATERIFF);
CheckMMIOError(Res); // Create the format chunk.
FormatChunkInfo.ckid = mmioStringToFOURCC("fmt ", 0);
FormatChunkInfo.cksize = sizeof(WAVEFORMATEX);
Res = mmioCreateChunk(handle, &FormatChunkInfo, 0);
CheckMMIOError(Res);
// Write the wave format data.
mmioWrite(handle, (char*)&WaveFormat, sizeof(WaveFormat)); // Create the data chunk.
Res = mmioAscend(handle, &FormatChunkInfo, 0);
CheckMMIOError(Res);
DataChunkInfo.ckid = mmioStringToFOURCC("data", 0);
DataSize = WaveHeader.dwBytesRecorded;
DataChunkInfo.cksize = DataSize;
Res = mmioCreateChunk(handle, &DataChunkInfo, 0);
CheckMMIOError(Res);
// Write the data.
mmioWrite(handle, (char*)WaveHeader.lpData, DataSize);
// Ascend out of the data chunk.
mmioAscend(handle, &DataChunkInfo, 0); // Ascend out of the RIFF chunk (the main chunk). Failure to do
// this will result in a file that is unreadable by Windows95
// Sound Recorder.
mmioAscend(handle, &ChunkInfo, 0);
mmioClose(handle, 0);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{ // Get recording parameters from radio group boxes on form.
// int samplesPerSec = (int)SamplesGroup->Items->Objects[SamplesGroup->ItemIndex];
int samplesPerSec = 44000;
// WORD channels = (WORD)(ChannelsGroup->ItemIndex 1);
WORD channels = 1;
// WORD bitsPerSample = WORD(8 * (BitsGroup->ItemIndex 1));
WORD bitsPerSample = 8;
DWORD avgBytesPerSec = channels * samplesPerSec; // Fill in the WAVEFORMATEX header.
WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
WaveFormat.nChannels = channels;
WaveFormat.nSamplesPerSec = samplesPerSec;
WaveFormat.nAvgBytesPerSec = avgBytesPerSec;
WaveFormat.nBlockAlign =
WORD((channels * bitsPerSample) / 8);
WaveFormat.wBitsPerSample = bitsPerSample;
WaveFormat.cbSize = 0; // Query device to see if it supports the selected format.
int Res = waveInOpen(&WaveHandle, WAVE_MAPPER,
&WaveFormat, 0, 0, WAVE_FORMAT_QUERY);
CheckWaveError(Res);
if (Res == WAVERR_BADFORMAT)
return; // Open device. Wave-in messages go to window proc of form.
Res = waveInOpen(&WaveHandle, WAVE_MAPPER, &WaveFormat,
MAKELONG(Handle, 0), 0, CALLBACK_WINDOW);
CheckWaveError(Res); // Allocate buffer for wave data large enough to hold data.
// int seconds = SecondsEdit->Text.ToIntDef(10);
int seconds = SecondsEdit->Text.ToIntDef(10);
int bufferSize = seconds * avgBytesPerSec;
if (WaveData)
delete[] WaveData;
WaveData = new char[bufferSize]; // Set up WaveHeader structure.
WaveHeader.dwBufferLength = bufferSize;
WaveHeader.dwFlags = 0;
WaveHeader.lpData = WaveData; // Prepare the header.
Res = waveInPrepareHeader(
WaveHandle, &WaveHeader, sizeof(WAVEHDR));
CheckWaveError(Res);
Res = waveInAddBuffer(
WaveHandle, &WaveHeader, sizeof(WAVEHDR)); // Error. Free the memory and exit.
if (Res != 0) {
waveInUnprepareHeader(
WaveHandle, &WaveHeader, sizeof(WAVEHDR));
delete[] WaveData;
WaveData = 0;
return;
} // Start recording.
Button2->Enabled = true;
Res = waveInStart(WaveHandle);
CheckWaveError(Res); }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// Call waveInReset() to stop recording. Forcec Windows to send
// the MM_WIM_DATA message to the application.
waveInReset(WaveHandle);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Button1->Enabled = false;
Button2->Enabled = false;
// Could have used low-level audio functions to play wave file
// but PlaySound is much easier.
//PlaySound("test.wav", 0, SND_FILENAME);
PlaySound((LPCSTR)WaveData,0,SND_MEMORY);
// PlaySound((LPCSTR)&WaveData,NULL,SND_MEMORY);
Button1->Enabled = true;
Button2->Enabled = true; }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
WaveData = 0;
Button2->Enabled = false;
}
發表人 - taishyang 於 2004/04/07 00:08:12