Skip to content

Sounds and Musics

Sygmei edited this page Nov 15, 2017 · 4 revisions

Playing sounds and musics in ÖbEngine is quite easy !

First of all, you need to understand what the difference between sound and music is.

A sound will be completely loaded in RAM whereas a music will be streamed from disk.

That's why using Sound class is appropriate for short audio files and Music class is better for long or voluminous audio files.

When you play a sound or a music, it is multithreaded, you don't have to wait for the playback to be over to resume script execution.

Sound class

Here is a minimal example to play a sound in ÖbEngine :

local sound = obe.Sound("hello.ogg");
sound.play();

Remember that paths used in Sound and Music class works exactly like System::Path class, it will look in every mounted paths.

If you want to load a sound after the object creation, you can also use the :load() method.

local sound = obe.Sound();
sound:load("hello.ogg");
sound:play();

Music class

Music class works exactly the same :

local music = obe.Music("song.ogg");
music:play();

Don't forget that the file is being streamed and must stay available during playback.

Also the Music class has a :getDuration() method that is not available on Sound class.

Playback control

You can control the playback for Sound and Music class with the following methods :

-- sound.ogg is a 5 seconds sound file
local sound = obe.Sound("sound.ogg"); -- Works exactly the same with obe.Music class
sound:play(); -- Starts the sound playback
print(sound:getStatus()); -- Displays "Play"
sleep(2); -- This method doesn't actually exists !
sound:pause(); -- Pauses the sound at ~2 seconds
print(sound:getStatus()); -- Displays "Pause"
sound:play(); -- Resumes the playback at ~2 seconds
sound:stop(); -- Stops the sound playback
print(sound:getStatus()); -- Displays "Stop"
sound:play(); -- Plays the sound at 0 seconds
sound:setPlayingOffset(obe.Seconds(4)); -- Sets playing offset at 4 seconds
sleep(1); -- Sound is now over
sound:setLooping(true); -- Sound will now loop
sound:play();
sleep(10); -- Sound will play twice during those 10 seconds
sound:stop();

Volume and pitch control

You can control the volume and the pitch of the Sound / Music using the following methods :

local sound = obe.Sound("sound.ogg");
sound:play(); -- Default volume is 100% and default pitch is 1.0
sound:setVolume(50); -- Set volume to 50%
print(sound:getVolume()); -- Displays "50"
sound:setVolume(10); -- Set volume to 10%
sound:setVolume(100); -- Set volume back to 100%
sound:setPitch(2); -- Set pitch to 2 (2x more acute than normal pitch)
print(sound:getPitch()); -- Displays "2"
sound:setPitch(0.5); -- Set pitch to 0.5 (2x more grave than normal pitch)
sound:setPitch(1); -- Set pitch back to 1 (Normal pitch)
Clone this wiki locally