Wednesday, October 6, 2010

Sound and music

There are two classes that deals with sound and music, one is located in BB.Sound, which is a wrapper around DirectSound, and the second one in BB.Sound.FMod which is a wrapper around the amazing FMod music and sound library (thanks guys!)

The first one is TSoundFactory, is a singleton class that helps the creating, loading and manipulation on a sound, also keeps a cache of the loaded sounds, the main methods are:

  • Initialize, guess...
  • Uninitialize, again...
  • LaunchTask, creates a TTask class with the loaded filename so you don't have to worry about this sound, once it finish it will be freed
  • New, creates a TSound class for a given criteria (size, stereo, rate and bits)
  • Load, loads a filename and returns it into a TSound
  • Load with streams and id
  • Clear, clear the factory
  • ToStereo, converts a TSound to stereo
  • ToMono, converts a TSound to mono
  • To16, converts a TSound to 16 bits
  • To8, converts a TSound to 8 bits
  • Copy, copies TSound from origin to destination
  • Mix, mixes a source of TSound into a single one
  • Find, search for a TSound with an id
  • Clone, clones a TSound (is much better to clone a TSound than to loaded twice)
The second class is the TSound, which is the sound buffer, its main methods are:
  • Generate, fills the buffer with a source pointer
  • Play, Stop, Pause, IsPlaying, guess...
  • Lock, returns a pointer to the buffer, so one is able to read or write on it
  • Unlock, every call to Lock must have an Unlock call
  • Length, length in bytes of the buffer
It also has a few properties like:
  • Position, where are we
  • Volume
  • Panning
  • Frequency
  • Loop, do we want to play over and over? (for sirens and so on...)
  • Stereo
  • Rate
  • Bits
Easy examples

1)
TSoundFactory.Load('laser.wav').Play;

2)
s: TSound;
p: PByteArray;

s := TSoundFactory.New(50000, False, 22050, 8);
p := s.Lock;
try
  //fill p with values in order to create a "melody"... 
finally
  s.Unlock;
end;

No comments:

Post a Comment