From: kazeroth on
Hi

So here it is. I tried to play mp3 using MCI and the thing is it's
noisy. I mean in the playback i hear kind of bumps in the bass
sections. And i thought about it, blamed the audio card of mine, but in
winamp the songs play allright. Furthermore the same song converted to
ogg or wma play ok, but as I had problems with wma hanging my program I
am having doubts about using less popular ogg format... Without the
proper drivers program just hangs itself.

So any ideas why the mp3 playback is "bumpy"? i tried to also blame the
compilator but the same compiled program works on the other computer
without the noises.

What others method do you suggest for mp3 playback? but no fmod or
sort, beacouse this is for commercial product.

Best Regards
Kazeroth

From: Bertel Brander on
kazeroth wrote:
> Hi
>
> So here it is. I tried to play mp3 using MCI and the thing is it's
> noisy. I mean in the playback i hear kind of bumps in the bass
> sections. And i thought about it, blamed the audio card of mine, but in
> winamp the songs play allright. Furthermore the same song converted to
> ogg or wma play ok, but as I had problems with wma hanging my program I
> am having doubts about using less popular ogg format... Without the
> proper drivers program just hangs itself.
>
> So any ideas why the mp3 playback is "bumpy"? i tried to also blame the
> compilator but the same compiled program works on the other computer
> without the noises.
>
> What others method do you suggest for mp3 playback? but no fmod or
> sort, beacouse this is for commercial product.

You might try with waveOutOpen and friends.
Something along the line of:

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>

typedef struct
{
WAVEFORMATEX wfx;
WORD wID;
DWORD fdwFlags;
WORD nBlockSize;
WORD nFramesPerBlock;
WORD nCodecDelay;
}MPEGLAYER3WAVEFORMAT;
#define MPEGLAYER3_WFX_EXTRA_BYTES 12
#define WAVE_FORMAT_MPEGLAYER3 0x0055

#define SAMPLE_RATE 22050
#define MP3_BLOCK_SIZE 522
#define MPEGLAYER3_FLAG_PADDING_OFF 0x00000002

void CALLBACK CallBackProc(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance,
DWORD dwParam1, DWORD dwParam2)
{
if(uMsg == WOM_DONE)
{
MMRESULT MMResult;
WAVEHDR *WaveHeader = (WAVEHDR *)dwParam1;

if((MMResult = waveOutUnprepareHeader(hwo, WaveHeader,
sizeof(WAVEHDR))) != MMSYSERR_NOERROR)
{
char Text[256];
waveOutGetErrorText(MMResult, Text, sizeof(Text));
printf("Failed to unprepare: %s\n", Text);
}

free(WaveHeader->lpData);
}
}

bool PlayMP3(const char* aFileName)
{
FILE *f = fopen(aFileName, "rb");
if(!f)
{
std::cerr << "Failed to open: " << aFileName << std::endl;
return 0;
}

HWAVEOUT WaveHandle;
WAVEHDR WaveHeader;
MMRESULT MMResult;

MPEGLAYER3WAVEFORMAT Mp3Format;
Mp3Format.wfx.cbSize = MPEGLAYER3_WFX_EXTRA_BYTES;
Mp3Format.wfx.wFormatTag = WAVE_FORMAT_MPEGLAYER3;
Mp3Format.wfx.nChannels = 1;
Mp3Format.wfx.nAvgBytesPerSec = 24 * 1024/8;
Mp3Format.wfx.wBitsPerSample = 0;
Mp3Format.wfx.nBlockAlign = 1;
Mp3Format.wfx.nSamplesPerSec = SAMPLE_RATE;
Mp3Format.fdwFlags = MPEGLAYER3_FLAG_PADDING_OFF;
Mp3Format.nBlockSize = 144 * 24*1024/SAMPLE_RATE; //MP3_BLOCK_SIZE;
Mp3Format.nFramesPerBlock = 1;
Mp3Format.nCodecDelay = 1393;
Mp3Format.wID = 1;

if(waveOutOpen(&WaveHandle, WAVE_MAPPER, (WAVEFORMATEX *)&Mp3Format,
(DWORD )CallBackProc, 0, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
{
std::cerr << "Failed to open Wave!" << std::endl;
return false;
}
memset(&WaveHeader, 0, sizeof(WaveHeader));
fseek(f, 0, SEEK_END);
long size = ftell(f);
std::cout << "Size of file: " << size << std::endl;
char *buffer = new char [size];
fseek(f, 0, SEEK_SET);
unsigned char Format[4];
fread(Format, 1, 4, f);
std::cout << std::hex << std::setw(2) << std::setfill('0') <<
(unsigned int)Format[0] << (unsigned int)Format[1] << (unsigned
int)Format[2] << (unsigned int)Format[3] << std::dec << std::endl;
size -= 4;
fread(buffer, 1, size, f);
WaveHeader.lpData = buffer;
WaveHeader.dwBufferLength = size;
WaveHeader.dwBytesRecorded = size;
WaveHeader.dwLoops = 1;
WaveHeader.dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP;

if(waveOutPrepareHeader(WaveHandle, &WaveHeader, sizeof(WaveHeader))
!= MMSYSERR_NOERROR)
{
printf("Failed to prepare header!\n");
return false;
}

if((MMResult = waveOutWrite(WaveHandle, &WaveHeader,
sizeof(WaveHeader))) != MMSYSERR_NOERROR)
{
char Text[256];
waveOutGetErrorText(MMResult, Text, sizeof(Text));
printf("Failed to write: %s\n", Text);
return false;
}
while(!(WaveHeader.dwFlags & WHDR_DONE))
Sleep(100);
if((MMResult = waveOutClose(WaveHandle)) != MMSYSERR_NOERROR)
{
char Text[256];
waveOutGetErrorText(MMResult, Text, sizeof(Text));
printf("Failed to close: %s\n", Text);
return false;
}
delete [] buffer;
return true;
}

int main(void)
{
PlayMP3("test.mp3");
return 0;
}

You need to adjust the sample rate etc. to match your mp3 file format.
This can be found in the first few bytes in the mp3 file.

/b

--
Just another homepage:
http://damb.dk
But it's mine - Bertel