NO

Author Topic: WaveOut API not working  (Read 10783 times)

leandrojardim

  • Guest
WaveOut API not working
« on: May 22, 2011, 12:22:23 AM »
I have Windows 7 Home Premium, and I am using Pelles C 6.50 to output a sine wave to the speakers using WaveOut API, but when I am going to write the 44.1 16 bit sample data using waveOutWrite, it returns the strange error "33". When I write an 8 bit sample data, it only output a very loud beep.

This error code exists in the Win32 API? I looked for it in the headers but not found a clue about it. Somebody knows what it means? Am I the only that gets this crazy error code on Windows 7?

CommonTater

  • Guest
Re: WaveOut API not working
« Reply #1 on: May 22, 2011, 12:43:34 AM »
If you look up the error in the mmsystem.h file it's telling you that it's not enabled...

I'm not sure what you would have to do to enable it, that might be device specific.  

Maybe some help here --> http://msdn.microsoft.com/en-us/library/ms713499(v=vs.85).aspx

Also, if you don't have the Windows SDK you may want to download it --> http://msdn.microsoft.com/en-us/windows/bb980924

The SDK is full Windows API documentation.


leandrojardim

  • Guest
Re: WaveOut API not working
« Reply #2 on: May 22, 2011, 03:47:19 AM »
Another problem, I am coding the sine wave again, too many errors to continue. Whats wrong with this code, you can see it for me? I have tried many times, some with different arguments but none work. The parameters for the Format structure were taken from another program. The parameters that the Win98 SDK gives also do not work. :(

Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#include <mmreg.h>

...

MMRESULT Result = 0;

WAVEFORMATEX Format;
HWAVEOUT Handle;

memset (&Format, 0, sizeof (WAVEFORMATEX));

Format.wFormatTag = WAVE_FORMAT_PCM;
Format.nChannels = 1;
Format.wBitsPerSample = 8;
Format.nSamplesPerSec = 44100;
Format.nBlockAlign = 1;
Format.nAvgBytesPerSec = 44100;
Format.cbSize = 0;

Result = waveOutOpen (&Handle, WAVE_MAPPER, &Format, (DWORD) SoundHandler, 0, CALLBACK_FUNCTION);
if (Result != MMSYSERR_NOERROR)
exit (Result);

I have read superficialy from some sites that Windows 7 have some problems with the WaveOut API. Somebody can say what are those problems and if they are related to this I am trying to do?

Thanks!

CommonTater

  • Guest
Re: WaveOut API not working
« Reply #3 on: May 22, 2011, 04:39:24 AM »
Right off... you should double check the call...
http://msdn.microsoft.com/en-us/library/ms713754(v=vs.85).aspx

I don't see anything blatently wrong.... So maybe one of the others can help?




leandrojardim

  • Guest
Re: WaveOut API not working
« Reply #4 on: May 22, 2011, 10:05:21 PM »
Thank you your help, CommonTater. :)

I dont speak english, sometimes I change "you could" by "you can", but this is not my purpose, and this looks a bit offensive, I know. :)

So, I am very glad you could help me! ;)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: WaveOut API not working
« Reply #5 on: May 23, 2011, 03:48:34 PM »
Small test program:
In Win 7 speakers/headphones must be connected.
Code: [Select]
#define WIN32_DEFAULT_LIBS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#include <mmreg.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
MMRESULT Result = 0;

WAVEFORMATEX Format;
HWAVEOUT Handle;
HWND SoundHandler = 0;

// nBlockAlign = nChannels * (wBitsPerSample \ 8)
// nAvgBytesPerSec = nBlockAlign * nSamplesPerSec
Format.wFormatTag = WAVE_FORMAT_PCM;
Format.nChannels = 1;
Format.nSamplesPerSec = 44100;
Format.nAvgBytesPerSec = 44100;
Format.nBlockAlign = 1;
Format.wBitsPerSample = 8;
Format.cbSize = 0;

Result = waveOutOpen (&Handle, WAVE_MAPPER, &Format, (DWORD)(UINT) SoundHandler, 0, CALLBACK_WINDOW);
if (Result != MMSYSERR_NOERROR) {
MessageBox(0, "Error", "Error", MB_ICONERROR|MB_OK);
//exit (Result);
} else MessageBox(0, "No Error", "OK", MB_ICONINFORMATION|MB_OK);
return 0;
}
May the source be with you

leandrojardim

  • Guest
Re: WaveOut API not working
« Reply #6 on: May 28, 2011, 02:19:16 AM »
Thanks timovjl.

Now suddendly it works, but I see the error 33 or 34 when I try to use waveOutWrite. The strange thing is that there is no error 33 or 34 in the SDK includes/headers nor the documentation. Somebody could test this code on your computer for me, please?

Note that you must close the application with the task manager.

Code: [Select]

#include <stdlib.h>
#include <math.h>
#include <stdio.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#include <mmreg.h>

#define CHANNEL 1
#define FREQUENCY 44100
#define PITCH 16

WAVEFORMATEX Format;
HWAVEOUT Handle;
WAVEHDR Header;
unsigned short Buffer = 0;

void CALLBACK SoundHandler(HWAVEOUT Handle, UINT Message, DWORD Instance, DWORD High, DWORD Low)
{
Buffer = FREQUENCY / 2;
};

int WINAPI WinMain(HINSTANCE Instance, HINSTANCE Previous, LPSTR Command, int Show)
{
int Result = 0;

memset(&Format, 0, sizeof(Format));

Format.wFormatTag = WAVE_FORMAT_PCM;
Format.nChannels = CHANNEL;
Format.nSamplesPerSec = FREQUENCY;
Format.wBitsPerSample = PITCH;
Format.nBlockAlign = Format.nChannels * Format.wBitsPerSample / 8;
Format.nAvgBytesPerSec = Format.nSamplesPerSec * Format.nBlockAlign;
Format.cbSize = 0;

Result = waveOutOpen(&Handle, WAVE_MAPPER, &Format, (DWORD)SoundHandler, (DWORD)GetModuleHandle(NULL), CALLBACK_FUNCTION);
if (Result != MMSYSERR_NOERROR) {
char Buffer[32];
snprintf(Buffer, 32, "Error no. %d at line %d", Result, __LINE__);
MessageBox(0, Buffer, "Error Message", MB_ICONERROR | MB_OK);
};

memset(&Header, 0, sizeof(Header));

Header.lpData = (char *)&Buffer;
Header.dwBufferLength = sizeof(Buffer);
Header.dwFlags = 0;

Result = waveOutPrepareHeader(Handle, &Header, sizeof(Header));
if (Result != MMSYSERR_NOERROR) {
char Buffer[32];
snprintf(Buffer, 32, "Error no. %d at line %d", Result, __LINE__);
MessageBox(0, Buffer, "Error Message", MB_ICONERROR | MB_OK);
};

while (1) {
if ((Header.dwFlags & WHDR_PREPARED) == WHDR_PREPARED || (Header.dwFlags & WHDR_DONE) == WHDR_DONE) {
Result = waveOutWrite(Handle, &Header, sizeof(Header));
if (Result != MMSYSERR_NOERROR) {
char Buffer[32];
snprintf(Buffer, 32, "Error no. %d at line %d", Result, __LINE__);
MessageBox(0, Buffer, "Error Message", MB_ICONERROR | MB_OK);
};
};
};

return 0;
};
« Last Edit: May 28, 2011, 02:33:15 AM by leandrojardim »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: WaveOut API not working
« Reply #7 on: May 28, 2011, 10:20:37 AM »
Modified error messages and loop
Code: [Select]
#define WIN32_DEFAULT_LIBS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
//#include <mmreg.h>
//#include <stdlib.h>
//#include <math.h>
#include <stdio.h>

#pragma lib "winmm.lib"

#define CHANNEL 1
#define FREQUENCY 44100
#define PITCH 16

WAVEFORMATEX Format;
HWAVEOUT Handle;
WAVEHDR Header;
unsigned short Buffer = 0;

char *WinMMErrorString(MMRESULT res)
{
static char szError[100];
waveOutGetErrorText(res, szError, sizeof(szError));
return szError;
}

void CALLBACK SoundHandler(HWAVEOUT Handle, UINT Message, DWORD Instance, DWORD High, DWORD Low)
{
Buffer = FREQUENCY / 2;
}

int WINAPI WinMain(HINSTANCE Instance, HINSTANCE Previous, LPSTR Command, int Show)
{
int Result = 0;

//memset(&Format, 0, sizeof(Format));

Format.wFormatTag = WAVE_FORMAT_PCM;
Format.nChannels = CHANNEL;
Format.nSamplesPerSec = FREQUENCY;
Format.wBitsPerSample = PITCH;
Format.nBlockAlign = Format.nChannels * Format.wBitsPerSample / 8;
Format.nAvgBytesPerSec = Format.nSamplesPerSec * Format.nBlockAlign;
Format.cbSize = 0;

Result = waveOutOpen(&Handle, WAVE_MAPPER, &Format, (DWORD)SoundHandler, (DWORD)GetModuleHandle(NULL), CALLBACK_FUNCTION);
if (Result != MMSYSERR_NOERROR) {
char Buffer[100];
snprintf(Buffer, sizeof(Buffer), "Error no. %d %s at line %d", Result, WinMMErrorString(Result), __LINE__);
MessageBox(0, Buffer, "Error Message", MB_ICONERROR | MB_OK);
return 1;
}

memset(&Header, 0, sizeof(Header));

Header.lpData = (char *)&Buffer;
Header.dwBufferLength = sizeof(Buffer);
Header.dwFlags = 0;

Result = waveOutPrepareHeader(Handle, &Header, sizeof(Header));
if (Result != MMSYSERR_NOERROR) {
char Buffer[32];
snprintf(Buffer, sizeof(Buffer), "Error no. %d at line %d", Result, __LINE__);
MessageBox(0, Buffer, "Error Message", MB_ICONERROR | MB_OK);
return 2;
}

do {
Result = waveOutWrite(Handle, &Header, sizeof(Header));
if (Result != MMSYSERR_NOERROR && Result != WAVERR_STILLPLAYING) {
char Buffer[100];
snprintf(Buffer, sizeof(Buffer), "Error no. %d %s at line %d", Result, WinMMErrorString(Result), __LINE__);
MessageBox(0, Buffer, "Error Message", MB_ICONERROR | MB_OK);
return 3;
}
Sleep(100);
} while(Result == MMSYSERR_NOERROR);
/*
while (1) {
if ((Header.dwFlags & WHDR_PREPARED) == WHDR_PREPARED || (Header.dwFlags & WHDR_DONE) == WHDR_DONE) {
Result = waveOutWrite(Handle, &Header, sizeof(Header));
if (Result != MMSYSERR_NOERROR) {
char Buffer[100];
snprintf(Buffer, sizeof(Buffer), "Error no. %d %s at line %d", Result, WinMMErrorString(Result), __LINE__);
MessageBox(0, Buffer, "Error Message", MB_ICONERROR | MB_OK);
return 3;
}
}
}
*/
return 0;
}
« Last Edit: May 28, 2011, 10:52:42 AM by timovjl »
May the source be with you

leandrojardim

  • Guest
Re: WaveOut API not working
« Reply #8 on: May 28, 2011, 09:32:43 PM »
Thanks again your time, timovjl! :)

Now I see that I should not rely on the examples that Microsoft put on the SDK, and see from a time to other on the list of functions that the SDK exposes.

Your help was invaluable for me to find these things, thanks!

CommonTater

  • Guest
Re: WaveOut API not working
« Reply #9 on: May 29, 2011, 04:26:05 AM »
Yeah Timo's pretty good at this stuff.

One thing you should be aware of... the SDK really only gives you examples of using the function you've looked up... there can be dozens of other functions involved before you get anything resembling a practical result...