5

I am trying to play wav file in C with allegro5 and I wrote below code:

#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>

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

    ALLEGRO_DISPLAY *display = NULL;


    if (!al_init()){
        fprintf(stderr, "failed to initialize allegro!\n");
        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;
    }


    al_install_audio();
    al_init_acodec_addon();

    ALLEGRO_SAMPLE *sample = al_load_sample("bomb.wav"); //sample always NULL

    al_reserve_samples(1);

    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_ONCE, 0);

    al_rest(10.0);

    al_destroy_display(display);
    al_destroy_sample(sample);
    return 0;
}

I debuged this in visual studio 2013 and sample always NULL. I am tried some variety format.

Ex: ALLEGRO_SAMPLE *sample = al_load_sample("\\bomb.wav");

ALLEGRO_SAMPLE *sample = al_load_sample("Resource Files\\bomb.wav");

ALLEGRO_SAMPLE *sample = al_load_sample("\\Resource Files\\bomb.wav");

ALLEGRO_SAMPLE *sample = al_load_sample("C:\\bomb.wav");//after copied in C drive

ALLEGRO_SAMPLE *sample = al_load_sample("C:/bomb.wav");

and etc.

I am confused. How can I succeed this problem. Thanks in advance. Sorry language.

dragosht
  • 3,237
  • 2
  • 23
  • 32
Osman Villi
  • 346
  • 2
  • 24
  • The first example works for me if I put "bomb.wav" in the current directory. Where is your sound file located relative to the current dir? Can the sound be played on your system (i.e. without using allegro?) – rcorre May 14 '15 at 12:54
  • Full path is: C:\Users\*************\Documents\Visual Studio 2013\Projects\AllegroSesTest\AllegroSesTest. Yes I am playing wav file with media player. – Osman Villi May 14 '15 at 13:44
  • Does `al_filename_exists` return true for the provided path? – rcorre May 14 '15 at 18:02
  • if (al_filename_exists("bomb.wav")){ printf("FILE IS READY"); } I show printf's message. But not played wav file. :/ @rcorre – Osman Villi May 14 '15 at 19:08
  • 1
    I solved this problem. I converted wav file to ogg file in here link: http://audio.online-convert.com/convert-to-ogg and then add to my project the ogg file(this is my sweety). :)) Thanks everybody. – Osman Villi May 15 '15 at 13:17
  • For future reference, if Allegro is compiled in debug mode (or with some sort of special flag), it dumps a logfile to Allegro.log. In Allegro.log, it'll often tell you if a file is missing, or the codec is failing to read the file, or the codec is installed wrong, etc. to enable logging with release mode call (BEFORE al_init): al_set_config_value(al_get_system_config(), "trace", "level", "info"); – Katastic Voyage Sep 16 '22 at 15:53

1 Answers1

3
ALLEGRO_SAMPLE *sample = al_load_sample("bomb.ogg"); //convert to ogg file your link

It is working for me.