Hello community. I hope that all is good for you. At the link above I read an interesting way in order to create from scratch a wav file. The code which is available at the end of the thread works as expected - and it generates a wav file according to its header. Now I have a question, but even searching on the web I cannot find any answer. I expose my request.
If I can write some data, I guess I could play them without any file creation. In the previous code, we needed to use PlaySound() or mciSendString() so as to play sound after its creation. If I don't want to open an external file, is there a way to play a buffer with wav data directly ?
An embedded resource wave is pre-created, what was requested was if it is possible to create a wave resource "on the fly" at run-time and be able to play it.
Prepend a WAV header to the start of the raw data and then pass a pointer to that chunk of data to PlaySound().
Playing an already created WAV, either as a disk file or embedded resource, is a lot easier to manage.
Hello. Thank you for your answers. Obviously my requests on Google were pretty bad. I missed the key word "memory" using instead "buffer". This is exactly what I need :)
I cannot point to the buffer and I don't understand why :
1 2 3 4 5 6 7
WaveGenerator wg(SAMPLE_RATE, SAMPLE_WIDTH, DURATION);
static uint32_t buff[BUFF_SIZE];
memset(&buff[0], (uint32_t)0, BUFF_SIZE); // init
// create wave and fill my buffer
wg.sineTransform(&buff[0], (and other stuff));
// trying to point at start of the buffer
PlaySound((LPCTSTR)(&buff[0]), NULL, SND_MEMORY);
But be aware that the buffer needs to contain a proper WAVE structure, not just "raw" PCM samples
You are right. I have my filled buffer, but I don't hear anything. However the same buffer, but saved as a file works well with mciSendString() - not PlaySound(). PCM is the key. Thanks for the link ++
Use function sf_open_virtual() in order to open a "virtual" WAVE file, which allows you to read WAVE file data from a memory buffer, or write WAVE file data to a memory buffer, as you can provide your own SF_VIRTUAL_IO.
I would definitely encourage newer programmers to write their own .wav readers & writers. Reading binary files correctly is a very important skill, and .wav is about as simple a format as you're likely to find.