Outils pour utilisateurs

Outils du site


allegro:addon_audio

====== Différences ====== Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

allegro:addon_audio [2011/10/31 15:55]
127.0.0.1 modification externe
allegro:addon_audio [2018/03/01 15:06] (Version actuelle)
mrhide Remove BIDIR
Ligne 1: Ligne 1:
-===== Audio =====+===== Allegro — Audio =====
  
  
-In this section we teach you about using audio. ​ This is not an advanced audio tutorial.+Incluez ces headers :
  
-==== Basic Example ====+<code c> 
 +#include <​allegro5/​allegro_audio.h>​ /* Pour jouer de la musique */ 
 +#include <​allegro5/​allegro_acodec.h>​ /* Pour charger de la musique encodée */ 
 +</​code>​
  
-Extending on the display examplewe will create a program that plays a music sample for ten seconds after which the display closes and stops the clip. Refer to the display tutorial for any code not covered in this tutorial. +Ensuiteaprès avoir initialisé Allegro (avec ** al_init**) initialisez les deux greffons, les fonctions doivent être absolument appelées dans cet ordre :
-<file c main.c>​ +
-#include <​stdio.h>​ +
-#include <​allegro5/​allegro.h>​ +
-#include <​allegro5/​allegro_audio.h>​ +
-#include <​allegro5/​allegro_acodec.h>​+
  
-int main(int argc, char **argv) +<code c> 
-+   ​if (!al_install_audio()) { 
- ALLEGRO_DISPLAY *display = NULL; +      // Fail 
- ALLEGRO_SAMPLE *sample=NULL;​ +   ​
-  +   ​ 
- if(!al_init())  +   ​if (!al_init_acodec_addon()) { 
- +      // Fail 
- fprintf(stderr,​ "​failed to initialize allegro!\n"​);​// It does load as this isn't printed to the console. +   }
- return -1; +
-+
-         +
-       if(!al_install_audio())  +
-+
- fprintf(stderr,​ "​failed to initialize audio!\n"​);​ +
- return -1; +
-+
- +
-        ​if(!al_init_acodec_addon()) ​ +
- +
- fprintf(stderr,​ "​failed to initialize audio codecs!\n"​);​ +
- return -1; +
-+
-  +
-  if (!al_reserve_samples(1))  +
-+
-                fprintf(stderr,​ "​failed to reserve samples!\n"​);​ +
- return -1; +
-+
-  +
- sample = al_load_sample( "​footstep.wav"​ ); +
- if (!sample)  +
-+
- printf( "Audio clip sample not loaded!\n"​ );  +
- return -1; +
-+
-  +
- display = al_create_display(640,​ 480); +
-  +
- if(!display)  +
-+
- fprintf(stderr,​ "​failed to create display!\n"​);​ +
- return -1; +
-+
- /* Loop the sample until the display closes. *+
-   al_play_sample(sample,​ 1.0, 0.0,​1.0,​ALLEGRO_PLAYMODE_LOOP,​NULL);​ +
- +
- al_clear_to_color(al_map_rgb(0,​0,​0));​ +
-  +
- al_flip_display();​ +
-  +
- al_rest(10.0);​ +
-  +
- al_destroy_display(display);​ +
- al_destroy_sample(sample);​ +
- return 0; +
-+
-</​file>​ +
- +
-==== Walk through ==== +
- +
-This walk through includes code from the previous Display example. Refer to the previous tutorial'​s walk through for information on code that is not explained here. +
- +
-<code c>   ​ +
-#include <​allegro5/​allegro_audio.h>​ +
-#include <​allegro5/​allegro_acodec.h></​code>​ +
- +
-**#include <​allegro5/​allegro_audio.h>​** will include the audio add on library\\ +
-**#include <​allegro5/​allegro_acodec.h>​** will include the library to add audio formats such as wav,​flac,​ogg +
- +
-<code c>  ALLEGRO_SAMPLE *sample=NULL;</​code>​ +
-Create a pointer named sample of the the type **ALLEGRO_SAMPLE**. ​ This creates space in memory for us to place the audio clip. \\ +
-We initialize it to **NULL** so that later if the assignment of the audio clip load sample fails the value of the pointer will be **NULL** and we will exit our program because the audio loading failed. +
- +
-<code c>​if(!al_install_audio())  +
-+
- fprintf(stderr,​ "​failed to initialize audio!\n"​);​ +
- return -1; +
-}</​code>​ +
-Install the audio subsystem. This is required to use audio. The acodec addon requires the audio subsystem to run. +
-The function **al_install_audio** returns true on success, false on failure. Thus if the audio subsystem install fails we will print failed to initialize audio to the terminal screen and exit the program with an error status -1. Should this function fail, it will return **NULL**. While failure is unlikely in this simple example, you are better off checking every single function that could fail for any errors. You may have fewer hidden problems when the program becomes more complicated. We will use this idea to check all setup code. +
- +
- +
- +
- +
-<code c>​if(!al_init_acodec_addon())  +
-+
- fprintf(stderr,​ "​failed to initialize audio codecs!\n"​);​ +
- return -1; +
-}+
 </​code>​ </​code>​
-Install the audio codec addon this function registers all known audio formats so functions like **al_load_sample** can use them. The available formats are depend on what libraries are available, the full set of recognised extensions is: .wav, .flac, .ogg, .it, .mod, .s3m, .xm. 
  
-<code c>if (!al_reserve_samples(1))  +Maintenant on peut charger des sons aux formats .wav.flac, .ogg, .it, .mod, .s3m, .xm (selon dépendanceset les jouer (à condition qu'un Display ait été créedans notre jeu.
-+
-         ​fprintf(stderr"​failed to reserve samples!\n"​);​ +
-         ​return -1; +
-+
-</​code>​ +
-Reserves a number of sample instancesand creates a default mixer if one doesn'​t existThis allows us to decide how many audio samples we will be creating for now we are only creating oneLater we might have 3 different samplesThe samples will all use the default mixer. +
- +
-<code c>sample = al_load_sample( "​footstep.wav" ); +
-if (!sample +
-+
- printf"Audio clip sample not loaded!\n" ​);  +
- return -1; +
-+
-</​code>​ +
-The function **al_load_sample** accepts a path and filename as a string. Here we are loading the wav audio file footstep.wav .  This doesn'​t play the sample it returns a memory address for the audio sample footstep.wav which gets assigned to the pointer sample. ​ If a sample isn't actually loaded we will print audio clip sample not loaded to the terminal screen and exit the program.+
  
 <code c> <code c>
-al_play_sample(sample,​ 1.0, 0.0,​1.0,​ALLEGRO_PLAYMODE_LOOP,​NULL);​+   ​ALLEGRO_SAMPLE *sample 
 +    
 +   ... 
 +    
 +   if (!(sample = al_load_sample("​music.wav"​))) { 
 +      // Fail 
 +   } 
 +    
 +   al_play_sample(sample,​ 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP,​ NULL); 
 +    
 +   ... 
 +    
 +   ​al_destroy_sample(sample);
 </​code>​ </​code>​
-The function **al_play_sample** is responsible for actually playing the sample.\\ 
-Parameters in order : 
-  * the sample we wish to play in this case it is named sample. 
-  * gain - relative volume at which the sample is played; 1.0 is normal. 
-  * pan - 0.0 is centered, -1.0 is left, 1.0 is right, or **ALLEGRO_AUDIO_PAN_NONE**. 
-  * speed - relative speed at which the sample is played; 1.0 is normal. 
-  * loop - **ALLEGRO_PLAYMODE_ONCE**,​ **ALLEGRO_PLAYMODE_LOOP**,​ or **ALLEGRO_PLAYMODE_BIDIR** 
-  * ret_id - if non-NULL the variable which this points to will be assigned an id representing the sample being played. 
  
-<code c> +La fonction **al_play_sample** prend en paramètres : 
-al_destroy_sample(sample);​ +  * Le gain — volume auquel est joué le fichier, 1.0 est le volume normal 
-</​code>​ +  * La balance — **0.0** pour centré, **-1.0** pour gauche et **1.0** pour droite 
-The function ​**al_destroy_sample** accepts a sample ​as a parameter and frees the memory used by the sample.+  La vitesse — de lecture, 1.0 pour la vitesse normale 
 +  ​La répétition : acceptant l'une de ces 3 valeurs 
 +    ​ALLEGRO_PLAYMODE_ONCE — joue le son une seule fois 
 +    * ALLEGRO_PLAYMODE_LOOP — joue le son en boucle 
 +  ​L'Id du sample ​— à récupérer pour effectuer des opération sur le sample
  
-On a final noteto compile this code you should be in the directory where your code is located you may use gcc.+Allegro permet aussi d'​enregistrer le son du microde gérer le mixer système et de gérer des streams audio (pour jouers des compositions de samples).
  
-<code bash> ​   gcc audioTest.c -o audioTest -L/usr/local/lib -lallegro -lallegro_main -lallegro_audio -lallegro_acodec<​/code>+C.F. [[http://liballeg.org/a5docs/trunk/​audio.html]]
  
-See also NO_LINK_HERE for details on compiling.+[[allegro:​addons|Liste des greffons]]
allegro/addon_audio.1320072915.txt.gz · Dernière modification: 2011/12/19 22:00 (modification externe)