I can play all three files just fine, but there is a short delay between that I would rather not be there.
I there a way to have no delay between the files playing?
I know playing the files this way is not what everyone would do, but before I try any of them can anyone tell me if it would be futile. ie Would I always have the 0.5 second gap between the files no matter what method I used?
And needless to say, are there any specific methods anyone would reccomend for this task?
I was going to suggest loading the file into memory too. But I'm not sure that it truly fixes the problem. Here's my code which loads a wav file into memory, then plays it.
I find there is still a short gap between each sound.
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <mmsystem.h>
#include <iostream>
#include <fstream>
#include <conio.h>
class Wave {
public:
Wave(char * filename);
~Wave();
void play(bool async=true);
bool isok();
private:
char * buffer;
bool ok;
HINSTANCE HInstance;
};
Wave::Wave(char * filename)
{
ok = false;
buffer = 0;
HInstance = GetModuleHandle(0);
ifstream infile(filename, ios::binary);
if (!infile)
{
std::cout << "Wave::file error: "<< filename << std::endl;
return;
}
infile.seekg (0, ios::end); // get length of file
int length = infile.tellg();
buffer = newchar[length]; // allocate memory
infile.seekg (0, ios::beg); // position to start of file
infile.read (buffer,length); // read entire file
infile.close();
ok = true;
}
Wave::~Wave()
{
PlaySound(NULL, 0, 0); // STOP ANY PLAYING SOUND
delete [] buffer; // before deleting buffer.
}
void Wave::play(bool async)
{
if (!ok)
return;
if (async)
PlaySound(buffer, HInstance, SND_MEMORY | SND_ASYNC);
else
PlaySound(buffer, HInstance, SND_MEMORY);
}
bool Wave::isok()
{
return ok;
}
int main(int argc, char *argv[]) {
std::cout << "Trying to play sound ...\n";
Wave one("sound1.WAV");
Wave two("sound2.WAV");
Wave three("sound3.WAV");
one.play(0);
two.play(0);
three.play(0);
std::cout << "press key to exit";
getch();
return 0;
}
At line 75-77 the sound is played, one after the other. If the parameter is omitted, like this,
75 76 77
one.play();
two.play();
three.play();
then all three sounds will play simultaneously. Perhaps introducing a suitable duration Sleep() between each call will allow the sound to start playing at the required moment.
Thanks Chervil. Your code actually played for me with a much shorter pause between the files, although it did it in a weird way.
When I tried your code originally a few hours ago, the pauses were still there, but I tried it again now and it worked near perfectly. Also closed C++ builder and reopened it etc to make sure it wasn't a fluke.
The only thing I think I changed was freeing up some space on my computer from 18GB free to 26Gb free.