Xojo Conferences

« MBS Xojo Conference i… | Home | Xojo Conferences and … »

Tip of day: Write WAV file

Sometimes you write an audio app in Xojo and need to write your audio data to a file. Using the WAV file format is very easy and this code is from an app we made today. Samples are stored in memoryblock in LittleEndian byte order and with stereo. The code to write WAV file looks like this:

Sub WriteWAV(f as FolderItem, m as MemoryBlock) // write samples in Int16 format with stereo to a WAV file dim b as BinaryStream = BinaryStream.Create(f, true) b.LittleEndian=true b.Write "RIFF" b.WriteInt32 6+4+16+4+m.Size // size of file b.Write "WAVE" b.Write "fmt " b.WriteInt32 16 // size of following data b.WriteInt16 1 // format, uncompressed b.WriteInt16 2 // 2 Channels b.WriteInt32 44100 // Samples per Second b.WriteInt32 44100*4 // Bytes per Second b.WriteInt16 4 // Block align, Size of Sample in Bytes b.WriteInt16 16 // bits per sample b.Write "data" b.WriteInt32 m.Size b.Write m return f End Sub
20 09 15 - 10:03
two comments

BinaryStream f not closed ? :)
rgb (Email) - 20 09 15 - 21:24

Well, Close is optional as it’s automatically closed by binary stream destructor at the end of the method.
Christian Schmitz (URL) - 20 09 15 - 21:42

Remember personal info?

Emoticons / Textile

Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.