Skip to content

LoadSound

Narta Xaymar Dirks edited this page Oct 28, 2026 · 7 revisions

Blitz3DBlitzPlusBlitzBasic

LoadSound

LoadSound:Int32 filename:String
Parameter Mode Type Default Description
filename In String required

Returns: A sound handle, or 0 if the file does not exist or cannot be read.

Description

Loads a sound file into memory and returns a handle that PlaySound uses. WAV, MP3, OGG and FLAC files can be loaded. Load sounds once at the start of the program and play them as often as needed.

The example writes a short beep as beep.wav first, so that it needs no other file. The other audio examples use this file.

Differences

  • MIDI , module and Windows Media files are not supported.
  • Without a sound device (or with the environment variable BLITZLLVM_HEADLESS set) a file that exists still gives a handle, but nothing is played and the play commands return channel 0.

Examples

MakeBeep("beep.wav")
Local snd = LoadSound("beep.wav")
If snd = 0 Then RuntimeError "beep.wav could not be loaded"
PlaySound snd
Delay 600

; Writes half a second of a 440 Hz tone as a 22050 Hz mono WAV file.
Function MakeBeep(path$)
	Local f = WriteFile(path)
	Local n = 11025
	Tag f, "RIFF" : WriteInt f, 36 + n * 2 : Tag f, "WAVE"
	Tag f, "fmt " : WriteInt f, 16 : WriteShort f, 1 : WriteShort f, 1
	WriteInt f, 22050 : WriteInt f, 44100 : WriteShort f, 2 : WriteShort f, 16
	Tag f, "data" : WriteInt f, n * 2
	For i = 0 To n - 1
		WriteShort f, Int(Sin(i * 440.0 * 360.0 / 22050.0) * 12000 * (1.0 - i / Float(n)))
	Next
	CloseFile f
End Function

Function Tag(f, s$)
	For i = 1 To Len(s)
		WriteByte f, Asc(Mid(s, i, 1))
	Next
End Function

Related Commands

FreeSound, Load3DSound, LoopSound, PlaySound, SoundPan, SoundPitch, SoundVolume

Home · Command Index



Differences
Language Features
BlitzBasic
Blitz3D
BlitzPlus

Clone this wiki locally