-1

好的,所以我一直在关注本教程:http ://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4422&lngWId=3

到目前为止,我已经完成了所有工作,直到我需要将程序加载到 .raw 音频文件中。

以下是相关代码:

LPSTR loadAudioBlock(const char* filename, DWORD* blockSize)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
DWORD size = 0;
DWORD readBytes = 0;
void* block = NULL;

//open the file

if((hFile = CreateFile((LPCWSTR)filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
    return NULL;

// get it's size, allocate memory, and then read it into memory

size = GetFileSize(hFile, NULL);
block = HeapAlloc(GetProcessHeap(), 0, size);
ReadFile(hFile, block, size, &readBytes, NULL);

CloseHandle(hFile);
*blockSize = size;
return (LPSTR)block;
}

然后是我调用它的主要函数:

int _tmain(int argc, _TCHAR* argv[])
{
HWAVEOUT hWaveOut; //device handle
WAVEFORMATEX wfx; //struct for format info
MMRESULT result; // for waveOut return values
LPSTR block;
DWORD blockSize;

// first let's set up the wfx format struct

wfx.nSamplesPerSec = 44100; // rate of the sample
wfx.wBitsPerSample = 16; //sample size
wfx.nChannels = 2; // 2 channels = stereo
wfx.cbSize = 0; // no extra info
wfx.wFormatTag = WAVE_FORMAT_PCM; //PCM format
wfx.nBlockAlign = (wfx.wBitsPerSample >> 3) * wfx.nChannels;
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;

// then let's open the device

if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, 0, 0, CALLBACK_NULL) != MMSYSERR_NOERROR)
{
    fprintf(stderr, "unable to open Wave Mapper device.\n");
    Sleep(1000);
    ExitProcess(1);
}

// if no errors then close it
printf("The Wave Mapper device was opened successfully!\n");

//load and play file
if((block = loadAudioBlock("ding.raw", &blockSize)) == NULL)
{
    fprintf(stderr, "Unable to load file\n");
    Sleep(1000);
    ExitProcess(1);
}
writeAudioBlock(hWaveOut, block, blockSize);
Sleep(1000);
waveOutClose(hWaveOut);
return 0;
}

每次我运行程序时,我都会得到:“无法加载文件”输出。我在与我的 exe 相同的目录中有“ding.raw”文件。我也尝试过将完整路径设置为“C://path”和“C:/path”,但是编译器只是给了我更多关于无法加载 pdb 文件的错误。

有任何想法吗?我正在使用 Visual Studio 2012 Professional IDE 和编译器。

4

2 回答 2

3

而不是使用标准char,你应该使用例如_TCHARLPCTSTR无处不在。这将使您传递的所有字符串和字符串指针都是正确的。

查看argvto 的参数_tmain,您会发现它使用_TCHARchar. 这是因为 Windows 支持普通字符和 Unicode 字符,这取决于几个宏。有关更多信息,请参见此处。

因此,要解决可能是您的问题(因为您没有得到实际的错误代码,请参阅我的评论GetLastError),您应该像这样更改函数:

void *loadAudioBlock(LPCTSTR filename, DWORD* blockSize)
{
    // ...

    if((hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
    return NULL;

    // ...
}

并这样称呼它:

// ...

void *block;

if((block = loadAudioBlock(_T("C:\\path\\ding.raw"), &blockSize)) == NULL)
{
    fprintf(stderr, "unable to open Wave Mapper device, error code %ld.\n", GetLastError());
    Sleep(1000);
    ExitProcess(1);
}

// ...

如您所见,我还更改了返回类型,因为该文件是二进制文件并且没有任何可读文本。

于 2012-09-02T09:31:12.730 回答
0
LPSTR loadAudioBlock(const char* filename, DWORD* blockSize)
{
     if((hFile = CreateFile(CA2T(filename), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
return NULL;
}

请参阅 ATL 转换宏: http: //msdn.microsoft.com/en-us/library/87zae4a3%28v=vs.80%29.aspx仅转换 const char* LPCWSTR 不起作用。

于 2012-09-02T08:49:06.543 回答