How to play a waveform

How do I play a calculated waveform, say, a sinusoid?

Regards, Strange

what OSes?

On Mac OSX 10.

I use declares to Apples Audio Toolbox Framework, a high level and fairly easy to use framework. You can use it to write a file out then open and play it in a Sound or MoviePlayer. Or with AudioQueue it can be played directly (some have said AudioQueue isn’t safe, I’ve never had a problem). If you’re interested I can post these classes sometime.

But if writing out and opening a file is satisfactory… AIFF and WAV have simple, uncompressed formats which are easy to write directly from Xojo with a BinaryStream.

If realtime playing is needed but AudioToolbox is too much, an even easier library to declare against is BASS. In this thread I used it to get microphone input, but playing out should be very similar.

For some $ MBS has the AudioToolbox functions and more. There might be other plugins? or frameworks, ffmpeg?

I think writing it as a WAVE file in a temporary location and then playing it using the Xojo sound object is the way to go.

Create a MemoryBlock with the amount of bytes of your waveform (aligned by 2 bytes, only important if your data is 24-bit and mono) plus 44 bytes. The waveform data is placed on the 44th byte. The first 44 bytes are the headers. The below assumes Little-Endian byte order and assumes your data is in Little-Endian. The text strings for Tags are written as-is (not little-endian backwards).

[code]offset amt description

0 4 ‘RIFF’
4 4 size of the Memoryblock minus 8
8 4 ‘WAVE’
12 4 'fmt ’
16 4 size of the 'fmt ’ chunk minus 8 (in this case always decimal 16)
20 2 type (1 if integers, 3 if floats)
22 2 amount of channels (1 for mono, 2 for stereo)
24 4 samplerate (usually decimal 44100)
28 4 samplerate * FrameSize
32 2 FrameSize ((BitRate/8) * channels)
34 2 BitRate (usually 16, sometimes 24, if your data is in floats it’s 32)
36 4 ‘data’
40 4 amount of bytes of your waveform[/code]

The only other thing to mention is that if your data is 24-but mono with an odd number of samples (in other words if your MemoryBlock is an odd size, not even), the size at offset 40 reflects the ODD number, but your MemoryBlock as a whole needs to be an even number of bytes. That is, you are adding a padding byte at the end of the memoryblock.

Then save the MemoryBlock to a file, and then play it using the Xojo Sound Object.

Xojo Sound Object Documentation

It sure would be nice if the Sound object supported taking a MemoryBlock, it would eliminate having to write a temp file. Also, the docs should note what sound formats the Xojo Sound object supports. I assume WAVE and AIFF but it’d be nice to know what thers are supported. Like, what if the FolderItem is a CAF file? And on Windows??? =)

Will and Garth,

Thanks very much for your advice. I shall try out both approaches.

Best regards,