TGC Codebase Backup



WAV sound loop functions and functions to read a WAV file's channels, sample rate, and length by Naphier

30th Jan 2013 1:50
Summary

BEWARE! If meta data such as artist's name, track title, etc. is stored in the beginning of the file these functions will not work. LoopWav(filename$,iNum_loops,iOffset,iVolume,iDe



Description

// Sound Function Library Test Driver
// Last revision: 2013-01-29
//
//-----------------------------------------------------------------------------

#include "SoundLibrary.agc"

// globals require for LoopWav()
global LoopsPlayed = 0
global LoopTimer = GetMilliseconds()
global sound_ID

// parameters
iNum_loops = 0
iOffset = -100
iVolume = 100
iDecay_rate = 5
filename$ = "sound3.wav"

repeat

Print("Channels: " + Str(GetWavChannels(filename$)))
Print("Sample Rate: " + Str(GetWavSampleRate(filename$)))
Print("Wav length: " + Str(GetWavLength(filename$)))

LoopsPlayed = LoopWav(filename$,iNum_loops,iOffset,iVolume,iDecay_rate)

Sync()

until GetSoundExists(sound_ID) = 0

Escape()

END


function Escape() // Pauses the termination of the program until ESC is pressed
escape_text = CreateText("Press ESC or Back to exit...")
SetTextColor(escape_text,255,255,255,255)
SetTextDepth(escape_text,10000)
SetTextMaxWidth(escape_text,100)
SetTextPosition(escape_text,0,(98-GetTextSize(escape_text)))
Sync()

repeat
until GetRawKeyPressed(27) = 1

DeleteText(escape_text)
endfunction



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    //-----------------------------------------------------------------------------
// This file and any accompanying data files are free to use.
//
// Sound Function Library
//      LoopWav(filename$,iNum_loops,iOffset,iVolume,iDecay_rate)
//          GetWavChannels(filename$)
//          GetWavSampleRate(filename$)
//          GetWavLength(filename$)
//
//
// Last revision: 2013-01-29
//
//-----------------------------------------------------------------------------
//
// wav file format structure courtesy of http://www.neurotraces.com/scilab/scilab2/node24.html
//
//  Field	bytes	format	contains
//  1	    0...3	str4	"RIFF" in ASCII
//  2	    4...7	int4	Total bytes minus 8
//  3	    8...15	str4	"WAVEfmt" Eighth character is a space
//  4   	16...19	int4	16 for PCM format
//  5   	20...21	int2	1 for PCM format
//  6   	22...23	int2	channels
//  7	    24...27	int4	sampling frequency
//  8   	28...31	int4	bytes per second
//  9   	32...33	int2	bytes by capture
//  10  	34...35	int2	bits per sample
//  11  	36:39	str4	"data"
//  12  	40:43	int4	bytes in data
//
//
//
//===================================================================================


//global LoopsPlayed = 0
//global LoopTimer = GetMilliseconds()
//global sound_ID

function LoopWav(filename$,iNum_loops,iOffset,iVolume,iDecay_rate)
    // LoopWav() returns the number of loops that have been played 'LoopsPlayed'
    //      which must be stored in a global variable so that the function
    //      can be called inside of the main loop and be updated properly.
    // A global variable for the loop timer and sound file ID must also be kept.
    //
    // Parameter Options
    // use 0 num_loops for infinite looping
    // set offset to increase or decrease the time between loops (milliseconds)
    // set volume for the initial volume of the sound
    // set decay_rate to the amount of volume reduction per loop
    //      if you have initial volume of 100 and 50 loops you might
    //      set the decay_rate to 2 for a smooth fade out

    // parameter error checks
    if GetFileExists(filename$) = 0
        Message("LoopWav file '" + filename$ + "' does not exist")
        end
    endif

    if iNum_loops < 0 or iVolume < 0 or iDecay_rate < -100 or iDecay_rate > 100
        Message("Parameter out of range in LoopWav()")
        end
    endif

    wav_time# = GetWavLength(filename$)

    if wav_time# + iOffset <= 0
        Message("Length of loop in LoopWav() is 0 or smaller")
        end
    endif

    // if this is the first call of the function then load the sound file
    if LoopsPlayed = 0
        sound_ID = LoadSound(filename$)
    endif

    // if the time since the last play of the sound is greater than or equal
    //      to the length of the wav file plus offset
    //      OR this is the first time the function is called
    if ((GetMilliseconds() - LoopTimer) >= (wav_time# + iOffset)) or LoopsPlayed = 0

        // set volume depending on loops played and decay rate
        iVolume = iVolume - iDecay_rate * LoopsPlayed

        // if sound decayed or iNum_loops have been played
        //  then quit function and delete sound
        if iVolume <= 0 or (LoopsPlayed > 0 and LoopsPlayed = iNum_loops)
            DeleteSound(sound_ID)
            exitfunction
        endif

        PlaySound(sound_ID,iVolume)
        inc LoopsPlayed
        LoopTimer = GetMilliseconds()
    endif

endfunction LoopsPlayed




function GetWavChannels(filename$)
// Returns the number of channels in a wav file.
// 1 for mono       2 for stereo

    if GetFileExists(filename$) = 0
        Message("GetWavChannels file '" + filename$ + "' does not exist")
        end
    endif

    wav_file = OpenToRead(filename$)

    for byte = 0 to 21
        ReadByte(wav_file)
    next byte

    channels = ReadByte(wav_file)
    channels = channels + ReadByte(wav_file)

    CloseFile(wav_file)

endfunction channels



function GetWavSampleRate(filename$)
// Returns the sample rate of the wav file in hertz

    if GetFileExists(filename$) = 0
        Message("GetWavSampleRate file '" + filename$ + "' does not exist")
        end
    endif

    wav_file = OpenToRead(filename$)

    for byte = 0 to 27
        ReadByte(wav_file)
    next byte

    sample_rate = ReadInteger(wav_file)

    CloseFile(wav_file)

endfunction sample_rate



function GetWavLength(filename$)
// Returns the length of the wav file in milliseconds

    if GetFileExists(filename$) = 0
        Message("GetWavSampleRate file '" + filename$ + "' does not exist")
        end
    endif

    channels = GetWavChannels(filename$)
    sample_rate = GetWavSampleRate(filename$)

    wav_file = OpenToRead(filename$)

    for byte = 0 to 39
        ReadByte(wav_file)
    next byte

    bytes_in_data = ReadInteger(wav_file)

    CloseFile(wav_file)

    wav_time# = bytes_in_data*1000.0 / (channels*sample_rate)

endfunction wav_time#