Intrinsic-141 PlaySoundData

Top  Previous  Next

141: PlaySoundData(Data, Size, Chans, Bits, SamplesPerSec, Align, Wait);


This intrinsic plays audio data stored in program memory through the PC speakers. The arguments control which data is played, its format and how it should be played back.


Data - This item contains the audio data. The data must be stored in a segment variable. See the example below.


Size - This item contains the size in bytes of the audio data.


Chans - This item specifies the number of channels of audio in the data. This would normally be one for monaural and two for stereo. If the sound is stereo, the samples for each channel are stored interleaved, with each sample alternating between the left and right channels.


Bits - This item specifies the number of bits per sample. Normally this is either 8 or 16 bits.


SamplesPerSec - This specifies the playback speed in samples per second. Normally the data is played back at the same speed it was recorded, but you can choose different speeds for special effects.


Align - This is the block alignment of complete data packets in bytes. In other words, it is the number of bytes need for a single sample for all channels. Generally this will be the number of Chans times the Bits per sample divided by 8.


Wait - This item controls whether the program waits for the sound to complete or returns immediately allowing the program to go on to do other tasks. If this Wait flag is false, any subsequent sound will be played immediately, mixing with any sounds that are already playing.


Warning: Be careful not to play a sound with the Wait flag to false just before exiting the program. If the sound is still playing when the program exits, the array containing the sound will be deallocated before the sound finishes, causing a memory error.


Here is an example of playing a synthesized 500 Hz sine wave generated on the fly:


inc        C:\CXPL\EXPLCodes.xpl;        \intrinsic declarations



seg char TestSnd(1);        \Array to contain the sound


def Pi = 3.1415924;

def SampleRate = 11025;        \Number of samples per second

def Seconds = 2;                \Number of seconds to play sound

def Freq = 500.0;                \Frequency of sound


def MemorySize = SampleRate * Seconds;                \Calculate Array Size

def Omega = (2.0 * Pi) / float(SampleRate);        \Calculate Radian/Sec


int I;



\Allocation memory for the sound



\Generate the sound wave form

for I:=0 to MemorySize-1 do        

 TestSnd(0,I):=Fix((Sin(Omega * Freq * Float(I)) * 127.5)+0.5)+127;


\Play the sound