Torque Sound / Audio

From TorqueWiki

Jump to: navigation, search

Contents

[edit] Links

[edit] Tutorials

http://tdn.garagegames.com/wiki/Audio/Profiles

http://tdn.garagegames.com/wiki/Audio/Getting_Started

http://www.gametheory.ch/index.jsp?positionId=13696

[edit] 2D Sound - In Game Musik

Hintergrundmusik ins MainMenu: http://tdn.garagegames.com/wiki/Audio/Music

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2729

[edit] GUI Sounds: Button Feedback

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=7562

Sound on mouseover: http://www.garagegames.com/mg/forums/result.thread.php?qt=28663


[edit] 3D AudioEmitter

http://www.garagegames.com/docs/tge/engine/classAudioEmitter.php

http://tdn.garagegames.com/wiki/Torque_Console_Objects_2#AudioEmitter

http://www.garagegames.com/mg/forums/result.thread.php?qt=22138

http://www.garagegames.com/docs/tge/engine/classAudioEmitter.php

[edit] WAV Streaming in Torque:

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2556

http://tdn.garagegames.com/wiki/Audio/Music

http://www.gametheory.ch/index.jsp?positionId=13725&displayOption=

[edit] Triggered Sound

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=9215

http://www.gametheory.ch/index.jsp?positionId=13701&displayOption=

[edit] Sound Ressourcen

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=category&qid=57

http://www.gametheory.ch/index.jsp?positionId=13749&displayOption=


[edit] Audio Scripting

Add more than one sound to an AudioProfile and play a sound at random when the AudioProfile is used:
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=12902

[edit] C++ Engine Level

[edit] MISC

VMPlayer in TGE: http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=9560

[~\engine\platform\platformAudio.h]

[edit] Sound Audio Tools

FMOD EX Firelight Technologies : http://www.garagegames.com/products/fmod

[edit] Sound Volume Control (low volume)

new AudioDescription(Gui_audioDescription) {

  volume   = 1.0; // or 100
  isLooping = false;
  is3D     = false;
  type     = $GuiAudioType;

};


http://www.garagegames.com/community/forums/viewthread/18000/

This fixed it for me: in prefs.cs: change $pref::Audio::channelVolume1 = .8; $pref::Audio::channelVolume2 = .8; $pref::Audio::channelVolume3 = .8; $pref::Audio::channelVolume4 = .8; $pref::Audio::channelVolume5 = .8; $pref::Audio::channelVolume6 = .8; $pref::Audio::channelVolume7 = .8; $pref::Audio::channelVolume8 = .8; ... $pref::Audio::masterVolume = .8;

TO

$pref::Audio::channelVolume1 = 1.0; etc.. .. $pref::Audio::masterVolume = 1.0;

edit note: This made the .wav file play as loud as it did in the WAV editor


http://www.garagegames.com/community/forums/viewthread/5887

Is there a way to change a specific sound volume on the fly?

new AudioDescription(AudioMusic) {

  volume   = 0.8;
  isLooping = true;
  isStreaming = true;
  is3D     = false;
  type     = $DefaultMusicType;

};

new AudioProfile(Mission_Music) {

     fileName = "~/data/sound/music/Track10.ogg";
     description = "AudioMusic";
     preload = "0";
  };

You have to use an own channel if you want to change the musics volume without changing the other sounds' volume.

alxSetChannelVolume($DefaultMusicType, 0.5);


http://www.garagegames.com/community/forums/viewthread/80509

volume slider control options, i.e., increase and decrease volume level in a game.


client/ui/optionsDlg.gui

  new GuiSliderCtrl(OptAudioVolumeSim) { // the type of sound you are modifying
           profile = "GuiSliderProfile";
           horizSizing = "right";
           vertSizing = "bottom";
           position = "107 172";
           extent = "240 34";
           minExtent = "8 8";
           visible = "1";
           variable = "value";
           altCommand = "OptAudioUpdateChannelVolume($SimAudioType, OptAudioVolumeSim.value);";
           helpTag = "0";
           range = "0.000000 1.000000";
           ticks = "8";
           value = "0.8";
        };




Music and Sound Channels


http://www.garagegames.com/community/forums/viewthread/79615

alxSetChannelVolume(0, 0.5); //50% volume on channel 0

...but everything is on channel zero. When I set the channel volume it does affect all currently playing sounds. How can I play audio on a particular channel. If I can keep music on channel zero and sound on channel one then I can adjust their volumes separately.


f you look in your client/scripts/audioProfiles.cs at the top you should see this variable: $GuiAudioType along with some others. These are just simply numbers that range from 1-8. I think you can have more than 8 channels but I'm not too sure. Anyways, If you create a variable called $SomeAudioType or whatever you want and set it to equal 3 or 4... then for your AudioDescriptions set the Type = "$SomeAudioType". That audioDescription will now use that sound channel. In your Options screen where people can adjust volumes, you may have to create a new field or some how manage it so that when the user changes a setting then in the options code part you will need to be changing that new $SomeAudioType as well. Ultimately to change the volume it's just alxSetChannelVolume(%shaaa, %BAAAMMM); Where %shaaa will be your channel, and %BAAAMMM will be the volume ranging from 0.0 to 1.0.



http://www.garagegames.com/community/forums/viewthread/57664

You shouldn't use datablocks for music, since a datablock has to be transfered from the server to the client. Use "new".

[edit] Audio Data

Place you sound files as .ogg or .wav under: ~\data\sound\

[edit] Audio Channels (type parameter)

You should (not required) add a new channel type for music, etc. to keep things seperate.

Use channels 1 - 8 for custom sound types, channel 0 is used in-game.

In client\scripts\audioProfiles.cs add to the existing channel list:

$MusicAudioType = xxx; 

like:

$GuiAudioType     = 1;
$SimAudioType     = 2;
$MessageAudioType = 3;
$MusicAudioType   = 4; 

[edit] Audio Description

The description contains details like the standard volume, looping, 3d sound, and audio type.

On the client use new in client\scripts\audioProfiles.cs, 2D-example:

new AudioDescription(MusicLooping)
{
  volume = 2.0;
  isLooping= true;
  is3D = false;
 type = $MusicAudioType;
};

On the server you need to define a datablock to create a networkable sound in server\scripts\audioProfiles.cs:

datablock AudioDescription( AudioDefault3d )
{
 volume   = 1.0;
 isLooping= false;
 is3D     = true;
 ReferenceDistance= 20.0;
 MaxDistance= 100.0;
 type = $SimAudioType;
};


[edit] Audio Profiles

The audio profile contains the information specific to the sound file. They link audio descriptors and data (sound files).

You may define them on the client and on the server in the same file as the AudioDescriptions: \scripts\audioprofiles.cs.

new AudioProfile( someMusic )
{
 filename = "~/data/sound/music/music.wav";
 description = "MusicLooping";
 preload = true;
};

In this case, the audio profile's name is "someMusic". Preload allows you to load the music up when the program is started. This will make the game run smoother since the song file is already loaded.


On the server:

datablock AudioProfile(audioName)
{
 filename = "~/data/sound/Audio.wav";
 description = "myaudioDescription";
 preload = true;
};


[edit] Client or Server - new or datablock

Use new on the client and datablock on the server to make the audio definitions networkable.

The following lists some debug code to test and compare both version.

"new" on the Client:

if( isObject(myClientAudioDesc) ) myClientAudioDesc.delete();
new AudioDescription( myClientAudioDesc )
{
  volume   = 1.0;
  isLooping= true;
  is3D     = false;
//  ReferenceDistance= 20.0;
//  MaxDistance= 100.0;
//  type = $SimAudioType;
  type = $MessageAudioType;
};


if( isObject(myClientAudioProf)  ) myClientAudioProf.delete();
new AudioProfile( myClientAudioProf )
{
  filename = "~/data/sound/footfall.wav";
  description = "myClientAudioDesc";
  //description = "myServerAudioDesc";
  preload = true;
};

Or define the datablock on the server:

datablock AudioDescription( myServerAudioDesc )
{
  volume   = 1.0;
  isLooping= true;
  is3D     = false;
//  ReferenceDistance= 20.0;
//  MaxDistance= 100.0;
//  type = $SimAudioType;
  type = $MessageAudioType;
  myField = "mydesc";
};

datablock AudioProfile( myServerAudioProf )
{
  filename = "~/data/sound/footfall.wav";
  description = "myServerAudioDesc";
  preload = true;
  myField = "myprof";
};

Test this from the mainMenuGui or from the playGui. Add a profile on the client and two buttons:

if(!isObject(musicButtonProfile)) 
  new GuiControlProfile( musicButtonProfile : GuiButtonProfile )
{
  soundButtonDown = "AudioButtonDown";
  soundButtonOver = "AudioButtonOver";
}; 

//--- OBJECT WRITE BEGIN ---

....// Main- or PlayGui.....

  new GuiButtonCtrl(startMusik) {
      canSaveDynamicFields = "0";
      Profile = "musicButtonProfile";  //<-- our profile
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "300 500";
      Extent = "140 30";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
     Command = "audioStart();";
      hovertime = "1000";
      text = "start music";
      groupNum = "-1";
      buttonType = "PushButton";
     variable = "23";
     myVar = "77";
   };

  new GuiButtonCtrl(stopMusik) {
      canSaveDynamicFields = "0";
      Profile = "musicButtonProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "450 500";
      Extent = "140 30";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
     Command = "audioStop();";
      hovertime = "1000";
      text = "stop music";
      groupNum = "-1";
      buttonType = "PushButton";
     variable = "23";
     myVar = "77";
   };

};
//--- OBJECT WRITE END ---

Function, called via the buttons, to start/stop the sound:

function audioStart()
{
  echo("_______audioStart()");

  echo("\nClient__Descriptor:" SPC myClientAudioDesc.getId() );
  echo("\Descriptor.volume:" SPC myClientAudioDesc.volume );

  echo("\nserver__Descriptor:" SPC myServerAudioDesc.getId() );
  echo("\Descriptor.volume:" SPC myServerAudioDesc.volume );
  echo("\Descriptor.myField:" SPC myServerAudioDesc.myfield );

  echo("\nserver__Profile:" SPC myServerAudioProf.getId() );
  echo("\nProfile.myField:" SPC myServerAudioProf.myfield );

  $myAudioHandle = 
    //  alxPlay( myClientAudioProf );
  alxPlay( myServerAudioProf );
    //alxPlay( myAudioProf );
//	     "~/data/sound/footfall.wav" );
}

function audioStop()
{
  echo("_________audioStop()" SPC $myAudioHandle );
  alxStop($myAudioHandle);
}

The server datablock definitions only work during a mission, thus from the playGui. Otherwise there is no connection to the server, thus there are no datablock definitions (from the server) present on the client.

[edit] 3D Sound - Audio Emitter

Via an AudioEmitter you place a 3D sound into the mission.

You find the Emitter in the World Creator: Mission Objects -> Environment -> AudioEmitter

new AudioEmitter(myMusic01) {
position = "77.3683 -222.465 3.8";
rotation = "1 0 0 0";
scale = "1 1 1";
profile = "GenericGameMusic";
useProfileDescription = "0";
description = "AudioDefaultLooping3d";
fileName = "~/data/sound/BGMusic.wav";
type = "1";
volume = "10";
outsideAmbient = "1";
referenceDistance = "1";
maxDistance = "100";
isLooping = "1";
is3D = "1";
loopCount = "-1";
minLoopGap = "0";
maxLoopGap = "0";
coneInsideAngle = "360";
coneOutsideAngle = "360";
coneOutsideVolume = "1";
coneVector = "0 0 1";
minDistance = "20.0";
};

[edit] Fields

Field Name Description Sample or Range
coneInsideAngle Sweep angle of inner cone. [0..360]
coneOutsideAngle Sweep angle of Outside cone. [0..360]
coneOutsideVolume Maximum gain in Zone C (see above). [0.0, 1.0]
coneVector Audio emitter’s eye (pointing) vector. information only (use visual feedback to direct sound)
Description Optional Audio Description Audio Description
enableVisualFeedback Enable visual effects showing audio-cones, facing, and on-off indication. [ false , true ]
fileName Relative or absolute path to sound file. Filename
is3D Enables 3D sound. [ false , true ]
isLooping Enable looping. [ false , true ]
loopCount -1 – Loop infinitely.

0 – Loop once and only once.
1 – Loop once, possibly twice.

(N > 1) – Loop N times.
See Description
maxDistance Outer boundary for 3D sound sphere. Sound turns on-off here at this distance from 3D emitter. [ referenceDistance , inf )
maxLoopGap Maximum delay between subsequent loop. -
minLoopGap Minimum delay between subsequent loop. -
outsideAmbient If true, plays outside only (turns off when player is inside interior). [ false , true ]
position Optional audio profile. AudioProfile
Profile Maximum gain distance. Inner-cone gain is maxed when player is within this distance of 3D emitter. [ 0.0 , maxDistance ]
ReferenceDistance Distance at which sound turns on to 100% of current maximum gain. [ 0.0 , inf.0 )
type Audio type. There can be multiple numeric audio types, each with its own globally controlled max gain. [ 0 , inf )
useProfileDescription Use audio profile instead of values set via inspector. [ false , true ]
volume Emitter’s maximum gain. [ 0.0 , 1.0 ]




[edit] GUI Sounds: Button Feedback

if(!isObject(musicButtonProfile)) 
  new GuiControlProfile( musicButtonProfile : GuiButtonProfile )
{
  soundButtonDown = "AudioButtonDown";
  soundButtonOver = "AudioButtonOver";
}; 

Define the profiles:

$GuiAudioType     = 1;
$SimAudioType     = 2;
$MessageAudioType = 3;
$MusicAudioType   = 4;

new AudioDescription(AudioGui)
{
   volume   = 1.0;
   isLooping= false;
   is3D     = false;
   type     = $GuiAudioType;
};


new AudioProfile(AudioButtonOver)
{
   filename = "~/data/sound/buttonOver.wav";
   description = "AudioGui";
   preload = true;
};

new AudioProfile(AudioButtonDown)
{
   filename = "~/data/sound/buttonDown.wav";
   description = "AudioGui";
   preload = true;
};

Use the defined profiles for some buttons (the button are used to start/stop music):

  new GuiButtonCtrl(startMusik) {
      canSaveDynamicFields = "0";
      Profile = "musicButtonProfile";  //<-- our profile
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "300 400";
      Extent = "140 30";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
     Command = "startMissionMusik();";
      hovertime = "1000";
      text = "start music";
      groupNum = "-1";
      buttonType = "PushButton";
     variable = "23";
     myVar = "77";
   };

  new GuiButtonCtrl(stopMusik) {
      canSaveDynamicFields = "0";
      Profile = "musicButtonProfile";
      HorizSizing = "right";
      VertSizing = "bottom";
      Position = "450 400";
      Extent = "140 30";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
     Command = "stopMissionMusik();";
      hovertime = "1000";
      text = "stop music";
      groupNum = "-1";
      buttonType = "PushButton";
     variable = "23";
     myVar = "77";
   };

[edit] Playing and Stopping the music

Now if you want to play the music defined in the AudioProfine "Music_MainMenu" add:

$musicHandle = alxPlay(Music_MainMenu);

Use $musicHandle is your handle to adjust that sound later on or to turn it off:

alxStop($musicHandle);

You may check if the music is on or off before you start or stop it:

function startMissionMusik()
{
  if (!alxIsPlaying($musicHandle)
    {
      $musicHandle = alxPlay(Music_MainMenu);
    }
}

function stopMissionMusik()
{
  if (alxIsPlaying($musicHandle)
    {
      alxStop($musicHandle);
    }
}

[edit] Music in Menus

Adding the music to loadMainMenu() will only play music when the menu first loads, not when it is returned to.

Instead add this function to to client\init.cs or create a new mainmenugui.cs and execute it in client\init.cs

function MainMenuGui::onWake(%this)
{
   // Play music...
   $musicHandle = alxPlay(Music_MainMenu);
}

That will play the music file to loop constantly when the main menu is loaded up.


To stop it again implement:

function MainMenuGui::onSleep(%this)
{
  // Stop backgound music.
  alxStop($musicHandle);
}

Or put in client\mission.cs:

alxStop($musicHandle);

Or in the function clientCmdMissionStart:

function clientCmdMissionStart(%seq) {

 alxStop($musicHandle);
 // The client recieves a mission start right before
 // being dropped into the game.

}

[edit] In Game Music Example

[edit] 2D-Sound: Example 1

Use %connection.play2D(profile):

new AudioProfile(pain)
{
filename = "~/data/sound/orc_death.ogg";
description = "NewSound";
preload = false;
};

LocalClientConnection.play2D(pain);

[edit] 2D-Sound Example 2

// music
[/data/sound/music]

// new channel for the music ?
[clients!]
[clients\scripts\audioProfiles.cs]

$DefaultAudioType = 0;
$GuiAudioType = 1;
$SimAudioType = 2;
// new
$MusicAudioType = 3;

// new Audio Description
// 1x
new AudioDescription(MusicLooping)
{
volume = 2.0;
isLooping= true;
is3D = false;
type = $MusicAudioType;
};

// new profile
// x-mal
new AudioProfile(Music_MainMenu)
{
filename = "~/data/sound/music/music.wav";
description = "MusicLooping";
preload = false;
};

// playing music
$musicHandle = alxPlay(Main_MenuMusic);

// example
[client\init.cs]
function loadMainMenu()
{
// Startup the client with the Main menu...
Canvas.setContent( MainMenuGui );
$musicHandle = alxPlay(Main_MenuMusic);
Canvas.setCursor("DefaultCursor");
}


// into a mission
[client\mission.cs]
alxStop($musicHandle);

function clientCmdMissionStart(%seq)
{
alxStop($musicHandle);
// The client recieves a mission start right before
// being dropped into the game.
}

[edit] Triggered Sound

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=9215

http://www.gametheory.ch/index.jsp?positionId=13701&displayOption=


function myTrigger::onEnterTrigger( %this, %trigger, %obj )
{
 $musicHandle = alxPlay(NewSoundStart);
 Parent::onEnterTrigger( %this, %trigger, %obj );
}


function myTrigger::onLeaveTrigger( %this, %trigger, %obj )
{
 alxStop($musicHandle);
 Parent::onLeaveTrigger( %this, %trigger, %obj );
}


[edit] C++ Engine Level

[~\engine\platform\platformAudio.h]

Personal tools
Games
Game Development
Modeling for Torque
Torque Game Builder (2D)
Console