问题描述
嗨!
我正在研究以下程序,从麦克风输入获取音频数据:
Hi!
Im working on following program, getting audio data from mic input:
#include <windows.h>
#include <mmsystem.h>
#include <mmreg.h>
#include <ksmedia.h>
//#include "stdafx.h" //ez a harom a fileba iras miatt kell
#include <iostream>
#include <fstream>
/* // additional sample code for definitions, doesnt help
#define _KSDATAFORMAT_SUBTYPE_ANALOG (GUID) {0x6dba3190,0x67bd,0x11cf,{0xa0,0xf7,0x00,0x20,0xaf,0xd1,0x56,0xe4}}
//#define KSDATAFORMAT_SUBTYPE_PCM (GUID) {0x00000001,0x0000,0x0010,{0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71}}
#define _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT (GUID) {0x00000003,0x0000,0x0010,{0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71}}
#define _KSDATAFORMAT_SUBTYPE_DRM (GUID) {0x00000009,0x0000,0x0010,{0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71}}
#define _KSDATAFORMAT_SUBTYPE_ALAW (GUID) {0x00000006,0x0000,0x0010,{0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71}}
#define _KSDATAFORMAT_SUBTYPE_MULAW (GUID) {0x00000007,0x0000,0x0010,{0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71}}
#define _KSDATAFORMAT_SUBTYPE_ADPCM (GUID) {0x00000002,0x0000,0x0010,{0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71}}
#define _KSDATAFORMAT_SUBTYPE_MPEG (GUID) {0x00000050,0x0000,0x0010,{0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71}}
#define _KSDATAFORMAT_SPECIFIER_VC_ID (GUID) {0xad98d184,0xaac3,0x11d0,{0xa4,0x1c,0x00,0xa0,0xc9,0x22,0x31,0x96}}
#define _KSDATAFORMAT_SPECIFIER_WAVEFORMATEX (GUID) {0x05589f81,0xc356,0x11ce,{0xbf,0x01,0x00,0xaa,0x00,0x55,0x59,0x5a}}
#define _KSDATAFORMAT_SPECIFIER_DSOUND (GUID) {0x518590a2,0xa184,0x11d0,{0x85,0x22,0x00,0xc0,0x4f,0xd9,0xba,0xf3}}
#define KSDATAFORMAT_SUBTYPE_PCM
//#define KSDATAFORMAT_SUBTYPE_PCM (GUID)
*/
using namespace std;
template <typename T>
void write(std::ofstream& stream, const T& t) {
stream.write((const char*)&t, sizeof(T));
}
int main()
{
const int NUMPTS = 44100 * 5; // 5 seconds
int sampleRate = 44100;
short int waveIn[NUMPTS]; // 'short int' is a 16-bit type; I request 16-bit samples below
// for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types
//string textout;
ofstream outputFile("auddata.txt", ios::trunc); //kiiras
HWAVEIN hWaveIn;
WAVEHDR WaveInHdr;
MMRESULT result;
// Specify recording parameters
/*
WAVEFORMATEX Format; // basic 16bit format - working
Format.wFormatTag=WAVE_FORMAT_PCM; //
Format.nChannels=1; // 1=mono, 2=stereo
Format.nSamplesPerSec=sampleRate; // 44100
Format.nAvgBytesPerSec=sampleRate*2; // = nSamplesPerSec * n.Channels * wBitsPerSample/8
Format.nBlockAlign=2; // = n.Channels * wBitsPerSample/8
Format.wBitsPerSample=16; // 16 for high quality, 8 for telephone-grade
Format.cbSize=0;
*/
///*
WAVEFORMATEXTENSIBLE pFormat; // 24 bit format - not working
pFormat.Format.wFormatTag=WAVE_FORMAT_EXTENSIBLE; // bõvített formátum lesz, késõbb
pFormat.Format.nChannels=1; // 1=mono, 2=stereo
pFormat.Format.nSamplesPerSec=sampleRate; // 44100
pFormat.Format.nAvgBytesPerSec=sampleRate*3; // = nSamplesPerSec * n.Channels * wBitsPerSample/8
pFormat.Format.nBlockAlign=3; // = n.Channels * wBitsPerSample/8
pFormat.Format.wBitsPerSample=24; // 16 for high quality, 8 for telephone-grade
//pFormat.Format.wValidBitsPerSample = 20;
pFormat.Format.cbSize=22;
pFormat.dwChannelMask=SPEAKER_FRONT_LEFT;
pFormat.SubFormat=KSDATAFORMAT_SUBTYPE_PCM;
//}
//*/
/* //sample code
WAVEFORMATPCMEX pFormat;
pFormat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
pFormat.Format.nChannels = 1;
pFormat.Format.nSamplesPerSec = 44100L;
pFormat.Format.nAvgBytesPerSec = 264600L; // Compute using nBlkAlign * nSamp/Sec
waveFormatPCMEx.Format.nBlockAlign = 6;
waveFormatPCMEx.Format.wBitsPerSample = 24; //Container has 3 bytes waveFormatPCMEx.Format.cbSize = 22;
waveFormatPCMEx.wValidBitsPerSample = 20; // Top 20 bits have data
waveFormatPCMEx.dwChannelMask = SPEAKER_FRONT_LEFT |
SPEAKER_FRONT_RIGHT |
// Stereo = 0x00000003
waveFormatPCMEx.SubFormat = KSDATAFORMAT_SUBTYPE_PCM; // Specify PCM
*/
result = waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat.Format,
0L, 0L, WAVE_FORMAT_DIRECT);
if (result)
{
char fault[256];
waveInGetErrorText(result, fault, 256);
//Application->
MessageBox(NULL, "Failed to open waveform input device.", NULL,
MB_OK | MB_ICONEXCLAMATION);
return(0);
}
// Set up and prepare header for input
WaveInHdr.lpData = (LPSTR)waveIn;
WaveInHdr.dwBufferLength = NUMPTS*2;
WaveInHdr.dwBytesRecorded=0;
WaveInHdr.dwUser = 0L;
WaveInHdr.dwFlags = 0L;
WaveInHdr.dwLoops = 0L;
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
// Insert a wave input buffer
result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
if (result)
{
MessageBox(NULL, "Failed to read block from device",
NULL, MB_OK | MB_ICONEXCLAMATION);
return(0);
}
// Commence sampling input
result = waveInStart(hWaveIn);
if (result)
{
MessageBox(NULL, "Failed to start recording",
NULL, MB_OK | MB_ICONEXCLAMATION);
return(0);
}
// Wait until finished recording
do {} while (waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR))==WAVERR_STILLPLAYING);
for( int a = 0; a < NUMPTS; a = a + 1 )
{
outputFile << a << ": " << waveIn[a] << endl;
}
outputFile.close();
MessageBox(NULL, "Recorded",
NULL, MB_OK | MB_ICONEXCLAMATION);
waveInClose(hWaveIn);
return(0);
}
16位输入适用于WAVEFORMATEX,但是24位我只是无法工作,得到以下错误:
KSDATAFORMAT_SUBTYPE_PCM未在此范围内声明
我在此消息上搜索了很多内容,但无法找到解决方案。可能存在一些微不足道的错误,因为我不是程序员。
16 bit input works fine with WAVEFORMATEX, but 24 bit i just cant get working, getting following error:
"KSDATAFORMAT_SUBTYPE_PCM was not declared in this scope"
I googled a lot on this message, but cant find a solution. There is propably some trivial mistake, since im not a programmer.
推荐答案
#include <tchar.h>
#include <ks.h> // KS.H must be included before KSMEDIA.H
#include <ksmedia.h>
为链接器选项添加了winmm.lib。
更改主要
Added winmm.lib to linker options.
Changed main
// int main()
int _tmain(int argc, _TCHAR* argv[])
声明多个文本为UNICODE
Declarated several Text as UNICODE
TCHAR fault[256];
waveInGetErrorText(result, fault, 256);
TEXT("Failed to start recording")
没有错误;没有警告。
运行后有一个文件auddata.txt,包含2,92 MB。
No error; no warning.
After run there is a file "auddata.txt" with 2,92 MB.
这篇关于KSDATAFORMAT_SUBTYPE_PCM未在此范围内声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!