FANDOM


Audiere Tutorial Edit

By Rohit Chauhan, Sarkaghat, Distt. Mandi, Himachal, INDIA Edit

Thx to Kornholijo for cleaning up code! Edit

Audiere is a high level API for C++ which is suitable to add sound functionality to your 2D game.Although every decent game library comes with sound functions of their own, I personally prefer integrating audiere to them because of the ease of use of this excellent audio library.I will be starting with making a Simple Console Music Player. This process will make you understand all the possibilities and of course, limitations of audiere. Bon Voyage !!!

Day ONE Edit

How to make a Music player with Audiere library and DevCPP. In just 5 minutes.

  1. Download Audiere-1.9.3-win32 for windows.It is 558 KB in size
  2. Extract it and place it somewhere.
  3. Make a New Folder, lets say, "player".
  4. Place "audiere.dll" and some .mp3 and/or .wav music files in it.
  5. Open DevCPP. Start a Console Project in the "player" directory.
  6. Open the "main.cpp" file of the project.
  7. Go to Project Options and in "Directories" state the include path of the "audiere.h" file. Again, here, in parameters add to the linker path \lib\audiere.lib
  8. Now, in the main.cpp, delete all what is written there.
  9. Copy and paste the following code:

Code

  1. include <string>
  2. include <cstdio>
  3. include "audiere.h"

using namespace audiere;

using namespace std;

int main(void) {

// This audio player is written in C++, using the Audiere API and classes. // If you have any questions or suggestions, email rohitchauhan802@sancharnet.in // or supernatural132003@yahoo.com

 	string filename;

cout << "Enter the filename of the audio file you want to play: " << endl; cin >> filename;

AudioDevicePtr device(OpenDevice()); OutputStreamPtr sound(OpenSound(device, filename.c_str(), false));

sound->play(); sound->setRepeat(true); sound->setVolume(1.0f);

getchar(); return 0; } Save the file and Press F9 to compile and run.

Remember - type full filename and if the file isnt in same dir as binary, type full path, etc c:/test/adsd.wav

    	The files should be only in the formats specified by Audiere.

Most of the code above should be understandable to a beginner, because I have used simple function to be straight forward.

For detailed understanding see at extreme below, the explanation of the above program from the documentation (extreme below) supplied by the Author himself "Chad Austin".

You can EMail me at rohitchauhan802@sancharnet.in or supernatural132003@yahoo.com for any other doubts Best luck. Enjoy AUDIERE !!!

Day TWO Edit

Changing Volume with the Keyboard

I am not straight away into designing something like WINAMP or Windows Media Player because I believe that first we should get hold of all the functions and logic in Console mode. Later , I will be adding articles on how to make a GUI for Audiere with FLTK ("Fast and Light GUI Toolkit ") which is my favorite GUI library . Though many people seem to like QT, but I think that FLTK is best, atleast for the beginners :)


This Simple 10 line code will make you increase the volume of the playing Song by Pressing any keyboard key again and again. With a little logic added, you should be easily able to decrease, mute and half-mute the volume.

  1. include <string>
  2. include <cstdio>
  3. include "audiere.h"

using namespace audiere; using namespace std;

int main(void) {

// This audio player is written in C++, using the Audiere API and classes.

// If you have any questions or suggestions, email rohitchauhan802@sancharnet.in // or supernatural132003@yahoo.com

string filename;

cout << "Enter the filename of the audio file you want to play: " << endl;

cin >> filename;


AudioDevicePtr device(OpenDevice()); OutputStreamPtr sound(OpenSound(device, filename.c_str(), false));

float volume = 0.1f;

sound->play(); sound->setRepeat(true);

while(volume < 1.1) {

sound->setVolume(volume); if (getchar())

               	volume += 0.01;
     	}
     	getchar();
     	return 0;

}

Press F9 to compile and run.Press any key number of times or keep it pressed to see as your volume increases.


Tip- You can place audiere.dll directly in the c:\windows\system32 directory to spare yourself hardwork every time placing audiere.dll with your source file.


Functions in the Library-

void play (); void stop (); bool isPlaying (); void reset ();

int getPosition (); void setPosition ( int pos );

float getVolume (); void setVolume ( float volume );

void setRepeat ( bool repeat ); bool getRepeat ();

void setPan ( float pan ); float getPan ();

bool isSeekable (); int getLength ();

void setPitchShift ( float shift ); float getPitchShift ();

Most of the functions are self explanatory.

  • setPan means your Speakers balance. -0.1 gives sound output only through the left Channel ( or Speaker ) and +0.1 gives the output through right channel (Speaker) only. All intermediate values give a mixed balance
  • setPithchShift sets tone.Value varies between 0.5 and 2.0, 1.0 is the default value. Volume range is between 0.0 to 1.0
  • getLength gives the length of your music file
  • isSeekable function tells us if your music file supports the setting of the position of your sound.setPosition method can be used to set to the desired position. Current position can always be known by calling the getPosition function

Day THREE Edit

Hope some of you managed to add some new functionality to your Music Player. For those who didnt manage to do it, I will be giving you a comphrehensive example on how to implement them exactly. I received a few E-mails asking me to continue the tutorial with Games perspective. I think that since audiere is suitable for 2D games, getting firmer grasp of function implementation through these type of articles will enable you to add sound as you like. Because music will all the same run behind the scene. I personally got audiere running with SDL, Clanlib and Irrlicht. So just cram them (functions) up and you can implement them to your taste in the Game Library of your choice.


Just copy the below typed example and play around with it Code-

// This code has errors and is not likely to work! 
  1. include "audiere.h"
  2. include <cstdio>
  3. include <iostream>

using namespace std; using namespace audiere;

int main(int argc,char *argv[]) {

   string fileName;
   cout << "Enter the Filename to play" << endl;
   cin >> fileName;
   AudioDevicePtr device(OpenDevice());
   OutputStreamPtr sound(OpenSound(device, fileName, false));
   float volume = 0.1;

   while(true)
   {
   	sound->setVolume(volume);
   	sound->play();
   	if(getchar() == 'i')
   		volume += 0.1;
   	if(getchar() == 'd')
   		volume -= 0.1;
   	if(getchar() == 'm')
   		volume=0.0;
   	if(getchar() == 'r')
   		sound->reset();
   	if (volume > 1.0)
   		volume = 1.0;
   	if (volume < 0.0)
   		volume = 0.0;
   }
   return 0;

}

Hope you got a hang of it !!!


Another Example by Johnathan Botha Edit

Thanx to Rohit Chauhan, Sarkaghat, Distt. Mandi, Himachal and Kornholijo for the tutorial above!

The example below is a simple media player to clarify the use of Audiere as described by the tutorial above. This example does not carry much weight in game-programming itself, but sheds some light on how to mute an audio stream and use the arrow keys as volume adjusters. The tutorial above works well, but adjusting the volume only works sometimes. The example below adjusts volume successfully every time you press the up- or down- arrow keys.


  1. include <cstdlib>
  2. include <iostream>
  3. include <conio.h>
  4. include "audiere.h"

using namespace audiere; using namespace std;

/*

 This simple program serves as an example of how to use Audiere.
 Please note that I used the conio library for non-standard C++ single character reads
 conio IS NOT ANSI STANDARD and should be avoided due to possible platform-independant compilation issues.
 I also used the Microsoft DOS 'cls' command to clear the screen, this will not work on GNU/Linux
 or Unix based operating systems, please modify the code according to your platform.
 When running the program, press 'p' to select a file to play
 then type in the path to the file and press 'Enter'
 For example: "Enter the filename of the audio file you want to play: C:\myMediaFile.mp3"
  • /


//This function plays the media file selected by the user, read the examples by //Rohit Chauhan, Sarkaghat, Distt. Mandi, Himachal above for further details.

int playMedia(string filename) {

   bool cont = true;
       //Open device
       AudioDevicePtr device(OpenDevice());
       //OutputStreamPtr sound(OpenSound(device, filename.c_str(), false)); -> this would be used to play a file directly from the hard-disk
       //Load media file into memory
       OutputStreamPtr stream(OpenSound(device, filename.c_str(), true));
	    if (!stream) {
           //Exit if stream could not be opened
	        exit(1);
	    }
	    
	    float volume = 0.1f;

bool muted = false; float preMuteVol = 0.0; char act;

       stream->setRepeat(true); //Enable repeat
       
       while(cont) {
                   
       stream->setVolume(volume);
       stream->play();
   
       act = getch();
       if(act == 'm')
       {
           //the line below enables the user to use mute by hitting 'm'.
           //It will save the volume before the first hit of 'm'
           //and restore it when 'm' is hit again.
           if(muted) {muted = false;volume=preMuteVol;} else {muted = true;preMuteVol = stream->getVolume();volume=0.0;}
       } 
       if(act == 'r') {
             stream->reset();
       }
       if(act == 'x') {
             //stop the stream and head back to the main menu.
             stream->stop();
             stream->reset();
             cont = false; //reset cont for next run.
             break;
       }
       if(act == 27)
       {
            //exit the program when escape is pressed (27)
            stream->stop();
            exit(0);
       }
       if(act == 72)
       {
           volume += 0.01; //Volume up when up-arrow pressed
       }
       if(act == 80)
       {
           volume -= 0.01; //Volume down when down-arrow pressed
       }
       if (volume > 1.0) 
       {
           volume = 1.0;
       }
       if (volume < 0.0)
       {
           volume = 0.0;
       }
   }

} int main(void) {

   char exit;
   while(exit != 27) {
       char ans;
       system("cls");
       //The main menu...
       cout << "             ---MENU---              " << endl;
       cout << "-------------------------------------" << endl;
       cout << "    Press 'p' to play a media file   " << endl;
       cout << "                                     " << endl;
       cout << "press the up arrow to increase volume" << endl;
       cout << "  down arrow key to decrease volume  " << endl;
       cout << "-------------------------------------" << endl;
       cout << "    Press ESC to exit the program    " << endl;
       ans = getch();
       if(ans == 'p')
       {
           system("cls");
           string filename;

cout << "Enter the filename of the audio file you want to play: "; cin >> filename; system("cls"); cout << "Playing: " << filename << " (press 'x' to return to main menu, 'r' to reset the current media, ESC to exit)" << endl; //call function to play media file and pass it the audio file as an argument... exit = playMedia(filename);

       }
       else if (ans = 27)
       {
            exit = 27;
       }
       else
       {
           cout << "You did not select a valid option" << endl;
           system("pause");
       }
    }
    return 0;

}

Remember to link your application and include the DLL file in your execution directory.

Tutorial from the Documentation Edit

This is the tutorial found in the audiere download, but made public so everyone can see how simple and awesome audiere is.

The text looks quite long, but it's almost 90% documentation. Starting audiere and playing a sound basically boils down to three functions.

/*
This is a very simple introduction to a very simple API.

Some basic concepts: Audiere has a C++ API.  You, as a user, interact
with Audiere through a set of abstract classes (i.e. interfaces)
defined in audiere.h.  All Audiere objects are reference counted.
This means that when you want to hold on to an object, you call ref()
on it, and when you are done, you call unref().  To make this easier,
Audiere provides a RefPtr class that calls ref() on construction and
unref() on destruction.  Otherwise, it acts just like a normal
pointer, greatly simplifying your code.  For even greater convenience,
each interface type has an associated InterfacePtr type which is just
like RefPtr<Interface>.

Now, on to the good stuff...

Make sure the Audiere header is in your compiler include path.  Every
Audiere symbol is contained within the namespace 'audiere', so if you
want your code to be a little clearer, you can bring the entire
audiere namespace into global scope.
*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#include "include/audiere.h"
using namespace audiere;


void main(){
	/*
	You need to open an AudioDevice before you can play sounds...
	*/

	AudioDevicePtr device(OpenDevice());
	if (!device) {
		// failure
	}

	/*
	Now that we have a device, we can actually open and play sounds.

	If OpenSound is called with the last parameter = false, then
	Audiere tries to load the sound into memory.  If it can't do
	that, it will just stream it.
	*/
	OutputStreamPtr sound(OpenSound(device, "1up.wav", false));
	if (!sound) {
		// failure
	}

	/*
	Since this file is background music, we don't need to load the
	whole thing into memory.
	*/
	//OutputStreamPtr stream(OpenSound(device, "1up.wav", true));
	//if (!stream) {
		// failure
	//}

	/*
	Great, we have some opened streams!  What do we do with them?
 
	let's start the background music first
	*/
	//stream->setRepeat(true);
	//stream->setVolume(0.5f); // 50% volume	
	//stream->play();

	/*
	now play a sound effect
	*/
	sound->play();

	/*
	Sleep a bit to let the sound finish
	*/
	Sleep(1000);

	/*
	When you are done using Audiere, just let the RefPtr objects go out of
	scope, and they will automatically clean themselves up.  If you really
	_must_ delete an object before its pointer goes out of scope, just set
	the pointer to 0.

	That's it!  Look in audiere.h or the doxygen documentation for
	descriptions of the remaining interfaces and methods.
	*/
}