don't click here

Allegro library problem

Discussion in 'Technical Discussion' started by saxman, Jan 8, 2012.

  1. saxman

    saxman

    Oldbie Tech Member
    I didn't want to clutter up ValleyBell's topic too much, so I decided to start a new topic. This relates to the problem LOst was talking about.

    I'm using Allegro v4.4. DIGI_AUTODETECT works fine. DIGI_DIRECTX(0) does not. The DirectX option has heavy audio distortion. A quick audio analysis reveals why it sounds bad. Here's what "silence" looks like:

    [​IMG]

    So everything that plays will get played on top of that. Can anyone explain this?




    Code (Text):
    1. #include <allegro.h>
    2.  
    3. AUDIOSTREAM *stream;
    4. short *allegroStream;
    5. volatile int timer = 0;
    6. volatile int timerChanged = 0;
    7. void TimerUpdate(void);
    8. const int testFrequency = 48000;
    9.  
    10.  
    11.  
    12. void TimerUpdate(void)
    13. {
    14.     timer++;
    15.     timerChanged = 1;
    16. }
    17. END_OF_FUNCTION(TimerUpdate);
    18.  
    19.  
    20.  
    21. int main()
    22. {
    23.     allegro_init();
    24.     install_keyboard();
    25.     install_timer();
    26.     //install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
    27.     install_sound(DIGI_DIRECTX(0), MIDI_AUTODETECT, NULL);
    28.  
    29.     set_color_depth(32);
    30.     set_gfx_mode(GFX_AUTODETECT_WINDOWED, 320, 240, 0, 0);
    31.  
    32.     LOCK_VARIABLE(timer);
    33.     LOCK_FUNCTION(TimerUpdate);
    34.     install_int_ex(&TimerUpdate, BPS_TO_TIMER(60));
    35.     stream = play_audio_stream(testFrequency/60, 16, 1, testFrequency, 255, 128);
    36.  
    37.     while(!key[KEY_ESC])
    38.     {
    39.         while(timerChanged == 0);
    40.         timerChanged = 0;
    41.  
    42.         allegroStream = (short*)get_audio_stream_buffer(stream);
    43.         if(allegroStream){
    44.             for(int I = 0 ; I < (testFrequency/60)*2 ; I++)
    45.             {
    46.                 allegroStream[I] = 0;
    47.             }
    48.             free_audio_stream_buffer(stream);
    49.         }
    50.     }
    51.  
    52.     remove_timer();
    53.     stop_audio_stream(stream);
    54.  
    55.     return 0;
    56. }
    57. END_OF_MAIN();
     
  2. winterhell

    winterhell

    Member
    1,165
    7
    18
    It seems that there is a problem with the timer function.
    When the timer says that 1/60 seconds have passed, DirectX is still playing the same chunk as the time you called free_audio_stream_buffer() . Thus probably get_audio_stream_buffer return NULL, if(allegroStream) fails, and you wait another 1/60 seconds till you try again.
    To solve it you probably need to either
    a) double the frequency of the timer to 120 BPS
    b) or move the line
    timerChanged=0;
    inside
    if(allegroStream)
    {
    stuff
    //here?
    }
    and increase the timer again, to 70 or more
    c) make a separate thread so it doesn't cpu hog the gameplay stuff

    Hope this helps.