1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/*
* Example program for the Allegro library.
*
* Test chaining mixers to mixers.
*/
#include <stdio.h>
#include "allegro5/allegro.h"
#include "allegro5/allegro_audio.h"
#include "allegro5/allegro_acodec.h"
#include "common.c"
char *default_files[] = {NULL, "data/haiku/water_0.ogg",
"data/haiku/water_7.ogg"};
int main(int argc, char **argv)
{
ALLEGRO_VOICE *voice;
ALLEGRO_MIXER *mixer;
ALLEGRO_MIXER *submixer[2];
ALLEGRO_SAMPLE_INSTANCE *sample[2];
ALLEGRO_SAMPLE *sample_data[2];
float sample_time;
float max_sample_time;
int i;
if (!al_init()) {
abort_example("Could not init Allegro.\n");
}
open_log();
if (argc < 3) {
log_printf("This example can be run from the command line.\nUsage: %s file1 file2\n", argv[0]);
argv = default_files;
argc = 3;
}
al_init_acodec_addon();
if (!al_install_audio()) {
abort_example("Could not init sound!\n");
}
voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16,
ALLEGRO_CHANNEL_CONF_2);
if (!voice) {
abort_example("Could not create ALLEGRO_VOICE.\n");
}
mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32,
ALLEGRO_CHANNEL_CONF_2);
submixer[0] = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32,
ALLEGRO_CHANNEL_CONF_2);
submixer[1] = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32,
ALLEGRO_CHANNEL_CONF_2);
if (!mixer || !submixer[0] || !submixer[1]) {
abort_example("al_create_mixer failed.\n");
}
if (!al_attach_mixer_to_voice(mixer, voice)) {
abort_example("al_attach_mixer_to_voice failed.\n");
}
for (i = 0; i < 2; i++) {
const char *filename = argv[i + 1];
sample_data[i] = al_load_sample(filename);
if (!sample_data[i]) {
abort_example("Could not load sample from '%s'!\n", filename);
}
sample[i] = al_create_sample_instance(NULL);
if (!sample[i]) {
abort_example("al_create_sample failed.\n");
}
if (!al_set_sample(sample[i], sample_data[i])) {
abort_example("al_set_sample_ptr failed.\n");
}
if (!al_attach_sample_instance_to_mixer(sample[i], submixer[i])) {
abort_example("al_attach_sample_instance_to_mixer failed.\n");
}
if (!al_attach_mixer_to_mixer(submixer[i], mixer)) {
abort_example("al_attach_mixer_to_mixer failed.\n");
}
}
/* Play sample in looping mode. */
for (i = 0; i < 2; i++) {
al_set_sample_instance_playmode(sample[i], ALLEGRO_PLAYMODE_LOOP);
al_play_sample_instance(sample[i]);
}
max_sample_time = al_get_sample_instance_time(sample[0]);
sample_time = al_get_sample_instance_time(sample[1]);
if (sample_time > max_sample_time)
max_sample_time = sample_time;
log_printf("Playing...");
al_rest(max_sample_time);
al_set_sample_instance_gain(sample[0], 0.5);
al_rest(max_sample_time);
al_set_sample_instance_gain(sample[1], 0.25);
al_rest(max_sample_time);
al_stop_sample_instance(sample[0]);
al_stop_sample_instance(sample[1]);
log_printf("Done\n");
/* Free the memory allocated. */
for (i = 0; i < 2; i++) {
al_set_sample(sample[i], NULL);
al_destroy_sample(sample_data[i]);
al_destroy_sample_instance(sample[i]);
al_destroy_mixer(submixer[i]);
}
al_destroy_mixer(mixer);
al_destroy_voice(voice);
al_uninstall_audio();
close_log(true);
return 0;
}
/* vim: set sts=3 sw=3 et: */
|