Game Development Community

dev|Pro Game Development Curriculum

Simplified Audio Profiles for T2D/iT2D

by Richard Ranft · 01/02/2013 (2:41 pm) · 1 comments

This code is to be dropped in audio.cs, right after the first two audio description definitions:
new AudioDescription(AudioChannel3)  
    {  
       volume = 1.0;  
       isLooping = true;  
       isStreaming=true;  
       is3D = false;  
       type = $musicAudioType;  
    };  
      
    new AudioDescription(AudioChannel4)  
    {  
       volume = 1.0;  
       isLooping = false;  
       isStreaming=true;  
       is3D = false;  
       type = $musicAudioType;  
    };  
      
    %file = findFirstFile("game/data/audio/*.wav");  
    while (%file !$= "")  
    {  
        %fileName = "game/data/audio/" @ fileName(%file);  
        echo(" @@@ creating AudioProfile for " @ %fileName);  
        %soundName = fileBase(%file);  
        %name = strreplace(%soundName, "loop", "");  
        if (%name !$= %soundName)  
            %desc = "AudioChannel1";  
        else  
            %desc = "AudioChannel2";  
        new AudioProfile(%name)  
        {  
            fileName=%fileName;  
            description=%desc;  
            preload=false;  
        };  
        %source = alxCreateSource(%name);  
        echo(" @@@ " @ %name.getName() @ ":" @%name.fileName @ ":" @ %source @ " created");  
        %file = findNextFile("game/data/audio/*.wav");  
    }  
      
    %file = findFirstFile("game/data/audio/*.mp3");  
    while (%file !$= "")  
    {  
        %fileName = "game/data/audio/" @ fileName(%file);  
        echo(" @@@ creating AudioProfile for " @ %file);  
        %soundName = fileBase(%file);  
        %name = strreplace(%soundName, "loop", "");  
        if (%name !$= %soundName)  
            %desc = "AudioChannel3";  
        else  
            %desc = "AudioChannel4";  
        new AudioProfile(%name)  
        {  
            fileName=%fileName;  
            description=%desc;  
            preload=false;  
        };  
        %source = alxCreateSource(%name);  
        echo(" @@@ " @ %name.getName() @ ":" @ %source @ " created");  
        %file = findNextFile("game/data/audio/*.mp3");  
    }

This will let you call alxPlay() to play your sound file. If you want a sound to loop, add "loop" to the file name.

For example, say you have background music for the menu. Name it menuMusic_loop.wav and put it in data/audio. Then, from your menu screen call alxPlay(menuMusic_loop); and your music will play, looping as intended because the script found "loop" in the file name and set up the profile accordingly.

Note that MP3's don't work under Windows - the engine does not support it.

An improvement would be to create a simset and add each profile to it as they are created, then save the simset. After that you could skip this load-time profile creating by simply executing the simset's saved file.

Also - you can ditch the echo()'s, they were mainly for troubleshooting.

About the author

I was a soldier, then a computer technician, an electrician, a technical writer, game programmer, and now software test/tools developer. I've been a hobbyist programmer since the age of 13.


#1
01/02/2013 (11:07 pm)
This is an awesome idea! Thank you!