/* Creates the default voice and mixer if they haven't been created yet. */ static bool create_default_mixer(void) { int voice_frequency = 44100; int voice_depth = ALLEGRO_AUDIO_DEPTH_INT16; int mixer_frequency = 44100; int mixer_depth = ALLEGRO_AUDIO_DEPTH_FLOAT32; ALLEGRO_CONFIG *config = al_get_system_config(); const char *p; p = al_get_config_value(config, "audio", "primary_voice_frequency"); if (p && p[0] != '\0') { voice_frequency = atoi(p); } p = al_get_config_value(config, "audio", "primary_mixer_frequency"); if (p && p[0] != '\0') { mixer_frequency = atoi(p); } p = al_get_config_value(config, "audio", "primary_voice_depth"); if (p && p[0] != '\0') { voice_depth = string_to_depth(p); } p = al_get_config_value(config, "audio", "primary_mixer_depth"); if (p && p[0] != '\0') { mixer_depth = string_to_depth(p); } if (!allegro_voice) { allegro_voice = al_create_voice(voice_frequency, voice_depth, ALLEGRO_CHANNEL_CONF_2); if (!allegro_voice) { ALLEGRO_ERROR("al_create_voice failed\n"); goto Error; } } if (!allegro_mixer) { allegro_mixer = al_create_mixer(mixer_frequency, mixer_depth, ALLEGRO_CHANNEL_CONF_2); if (!allegro_mixer) { ALLEGRO_ERROR("al_create_voice failed\n"); goto Error; } } /* In case this function is called multiple times. */ al_detach_mixer(allegro_mixer); if (!al_attach_mixer_to_voice(allegro_mixer, allegro_voice)) { ALLEGRO_ERROR("al_attach_mixer_to_voice failed\n"); goto Error; } return true; Error: if (allegro_mixer) { al_destroy_mixer(allegro_mixer); allegro_mixer = NULL; } if (allegro_voice) { al_destroy_voice(allegro_voice); allegro_voice = NULL; } return false; }
int main(int argc, char **argv) { ALLEGRO_VOICE *voice; ALLEGRO_MIXER *mixer; ALLEGRO_SAMPLE_INSTANCE *sample; int i; char const **filenames; int n; if (argc < 2) { n = 1; filenames = malloc(sizeof *filenames); filenames[0] = "data/testing.ogg"; } else { n = argc - 1; filenames = malloc(sizeof *filenames * n); for (i = 1; i < argc; ++i) { filenames[i - 1] = argv[i]; } } if (!al_init()) { abort_example("Could not init Allegro.\n"); } open_log(); 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); if (!mixer) { abort_example("al_create_mixer failed.\n"); } if (!al_attach_mixer_to_voice(mixer, voice)) { abort_example("al_attach_mixer_to_voice failed.\n"); } sample = al_create_sample_instance(NULL); if (!sample) { abort_example("al_create_sample failed.\n"); } for (i = 0; i < n; ++i) { ALLEGRO_SAMPLE *sample_data = NULL; const char *filename = filenames[i]; float sample_time = 0; /* Load the entire sound file from disk. */ sample_data = al_load_sample(filename); if (!sample_data) { abort_example("Could not load sample from '%s'!\n", filename); continue; } if (!al_set_sample(sample, sample_data)) { abort_example("al_set_sample_instance_ptr failed.\n"); continue; } if (!al_attach_sample_instance_to_mixer(sample, mixer)) { abort_example("al_attach_sample_instance_to_mixer failed.\n"); goto done; } /* Play sample in looping mode. */ al_set_sample_instance_playmode(sample, ALLEGRO_PLAYMODE_LOOP); al_play_sample_instance(sample); sample_time = al_get_sample_instance_time(sample); log_printf("Playing '%s' (%.3f seconds) 3 times", filename, sample_time); al_rest(sample_time); if (!al_set_sample_instance_gain(sample, 0.5)) { abort_example("Failed to set gain.\n"); } al_rest(sample_time); if (!al_set_sample_instance_gain(sample, 0.25)) { abort_example("Failed to set gain.\n"); } al_rest(sample_time); al_stop_sample_instance(sample); log_printf("\nDone playing '%s'\n", filename); /* Free the memory allocated. */ al_set_sample(sample, NULL); al_destroy_sample(sample_data); } al_destroy_sample_instance(sample); al_destroy_mixer(mixer); al_destroy_voice(voice); al_uninstall_audio(); done: close_log(true); return 0; }
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 needs to be run from the command line.\nUsage: %s file1 file2\n", argv[0]); goto done; } 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(); done: close_log(true); return 0; }
int main(int argc, char **argv) { const char *filename = "../demos/cosmic_protector/data/sfx/title_music.ogg"; ALLEGRO_VOICE *voice; ALLEGRO_MIXER *mixer; ALLEGRO_AUDIO_STREAM *stream; if (argc > 1) { filename = argv[1]; } if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_init_primitives_addon(); al_init_image_addon(); al_init_acodec_addon(); al_install_keyboard(); display = al_create_display(640, 480); if (!display) { abort_example("Could not create display.\n"); return 1; } dbuf = al_create_bitmap(640, 480); bmp = al_load_bitmap("data/mysha.pcx"); if (!bmp) { abort_example("Could not load data/mysha.pcx\n"); return 1; } if (!al_install_audio()) { abort_example("Could not init sound.\n"); return 1; } voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2); if (!voice) { abort_example("Could not create voice.\n"); return 1; } mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); if (!mixer) { abort_example("Could not create mixer.\n"); return 1; } if (!al_attach_mixer_to_voice(mixer, voice)) { abort_example("al_attach_mixer_to_voice failed.\n"); return 1; } stream = al_load_audio_stream(filename, 4, 2048); if (!stream) { abort_example("Could not load '%s'\n", filename); return 1; } al_set_audio_stream_playmode(stream, ALLEGRO_PLAYMODE_LOOP); al_attach_audio_stream_to_mixer(stream, mixer); al_set_mixer_postprocess_callback(mixer, update_meter, NULL); main_loop(); al_destroy_audio_stream(stream); al_destroy_mixer(mixer); al_destroy_voice(voice); al_uninstall_audio(); al_destroy_bitmap(dbuf); al_destroy_bitmap(bmp); return 0; }