Playing Longer Audio: Streaming

If we have a longer audio file, like one that contains a full-length song, we may want to load our file via the LoadToStream action. The LoadToStream action allows our audio file to be loaded in "chunks,"as they are needed instead of all at once. This is more efficient and allows the file to start playing more quickly than if it must wait for the entire audio file to be loaded, especially for longer sounds, like songs. In order to stream our Audio object, we will need a loop that keeps the Audio object streaming as long as it is playing. An example of this follows:

use Libraries.Sound.Audio

// this line creates our Audio object we will be using to stream
Audio streamingAudio

// this line loads a "chunk"of the data from the file "LongSong.ogg"to our Audio object
streamingAudio:LoadToStream("Sounds/longSong.ogg")

// this line plays the first "chunk"of the data that was loaded from the file
streamingAudio:Play()

// this loop will continuously check if the whole song has been played
repeat while streamingAudio:IsPlaying()
   // this gets the next "chunk"of data to play
   streamingAudio:Stream()
end

A Quorum File object can also be used instead of a text file path when calling the LoadToStream action.

If we are playing sound as part of a game using the Quorum Game Engine, the proper place to put the Stream() call is inside the Update action, which is called every frame in the Main Game Loop. This will ensure that the next "chunk"of sound will be loaded at every opportunity. An example of streaming in the game engine follows:

use Libraries.Game.Game
use Libraries.Sound.Audio

class Main is Game
   Audio audio1
   Audio song

   action Main
       StartGame()
   end

   action CreateGame
       audio1:Load("Sounds/Fwip.ogg")
       audio1:Play()
       song:LoadToStream("Sounds/song.ogg")
       song:Play()
   end

   action Update(number seconds)
       //since this action is in the main game loop and called every frame it should not be inside another loop.
       song:Stream()
   end
end

Activity: Playing Longer Audio

The same thing but with a longer audio file

Next Tutorial

In the next tutorial, we will discuss Digital Signal Processing, which describes signals and samples in Quorum..