Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Audio seems overly complicated :(

This thread is locked; no one can reply to it. rss feed Print
 1   2   3   4 
Audio seems overly complicated :(
bamccaig
Member #7,536
July 2006
avatar

Piano.zip has the source, media, and binary I made that shows this.

Thanks. That is working much better in Windows (assuming by Piano.zip you meant BCPiano.zip). I haven't tried playing with the driver options in Linux yet.

The source code in the zip appears to be the fancy, low-level version. Did the original version work too?

Quote:

Attached is your second program, with the modifications I mentioned.

It appears all you changed was the depth and then hard-coded filenames instead of generating them. I assume is should also work with the default voice and mixer, but did you try it?

Quote:

So it seems you can still attach sample instances to the mixer created by al_reserve_samples, regardless of the number of instances you reserve. I used al_reserve_samples(1) and attached multiple sample instances and they all played without distortion or dropping out.

My guess without trying to make sense of the code is that al_reserve_samples() is setting up a pool of a fixed size based on what you specify, and it automatically will use that to allocate instances from. As far as I've come across, I don't know of a limitation to the number of things that can be attached to a mixer. Though I'm also not sure if there are quality or performance limitations as a result. I assume there must be, or again, a "simple" API that hides all of these shenanigans should be braindead simple to wire up..

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

bamccaig said:

assuming by Piano.zip you meant BCPiano.zip

No, I meant Piano.zip, the link was broken. It's fixed now. That contains the first modified version you used.

bamccaig said:

I assume is should also work with the default voice and mixer, but did you try it?

Yes, it worked fine.

bamccaig said:

a "simple" API that hides all of these shenanigans should be braindead simple to wire up..

I want to know what exactly you mean. Write out the function prototypes you would expect a "simple" API to have.

Izual
Member #2,756
September 2002
avatar

bamccaig, here is my edited version of your piano using VOICE / MIXER / SAMPLE INSTANCES:

ZIP with source and all files here.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_acodec.h> 3#include <allegro5/allegro_audio.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_ttf.h> 6#include <stdio.h> 7#include <stdlib.h> 8 9#define notes_length ( sizeof( note_s ) / sizeof( ALLEGRO_SAMPLE * ) ) 10 11#define say_center(w, h, text) \ 12 (al_draw_text(musicals, white, w, h, ALLEGRO_ALIGN_CENTRE, text)) 13 14void play_note( ALLEGRO_SAMPLE_INSTANCE *si ) 15{ 16 /* 17 Setting position just to skip the silence at the start of the notes. 18 Why 32000 ? Because it sounds just right to me. Try more or less and see. 19 */ 20 al_set_sample_instance_position( si, 32000 ); 21 al_set_sample_instance_playing( si, true ); 22} 23 24static const char abcdefg[] = "abcdefg"; 25 26int main(int argc, char * argv[]) 27{ 28 char tmp[8] = " "; 29 int i = 0, l = -1; 30 int status = 0; 31 ALLEGRO_COLOR black, white; 32 ALLEGRO_DISPLAY * display = NULL; 33 ALLEGRO_EVENT_QUEUE * event_queue = NULL; 34 ALLEGRO_FONT * musicals = NULL; 35 ALLEGRO_TIMEOUT timeout; 36 ALLEGRO_VOICE *voice = NULL; 37 ALLEGRO_MIXER *mixer = NULL; 38 ALLEGRO_SAMPLE *note_s[ 7 ] = { 0 }; 39 ALLEGRO_SAMPLE_INSTANCE *note_si[ 7 ] = { 0 }; 40 41 if (!al_init()) 42 { 43 fprintf(stderr, "al_init failed\n"); 44 goto error; 45 } 46 if (!al_install_keyboard()) 47 { 48 fprintf(stderr, "al_install_keyboard failed\n"); 49 goto error; 50 } 51 if (!al_install_audio()) 52 { 53 fprintf(stderr, "al_install_audio failed\n"); 54 goto error; 55 } 56 if (!al_init_acodec_addon()) 57 { 58 fprintf(stderr, "al_init_acodec_addon failed\n"); 59 goto error; 60 } 61 if (!al_init_font_addon()) 62 { 63 fprintf(stderr, "al_init_font_addon failed\n"); 64 goto error; 65 } 66 if (!al_init_ttf_addon()) 67 { 68 fprintf(stderr, "al_init_ttf_addon failed\n"); 69 goto error; 70 } 71 display = al_create_display(1440, 900); 72 if (display == NULL) 73 { 74 fprintf(stderr, "al_create_display failed\n"); 75 goto error; 76 } 77 event_queue = al_create_event_queue(); 78 if (event_queue == NULL) 79 { 80 fprintf(stderr, "al_create_event_queue failed\n"); 81 goto error; 82 } 83 84 /* Setup sound: */ 85 voice = al_create_voice( 44100, 86 ALLEGRO_AUDIO_DEPTH_INT16, 87 ALLEGRO_CHANNEL_CONF_2 ); 88 if( !voice ) 89 { 90 fprintf(stderr, "al_create_voice failed\n"); 91 goto error; 92 } 93 mixer = al_create_mixer( 44100, 94 ALLEGRO_AUDIO_DEPTH_INT16, 95 ALLEGRO_CHANNEL_CONF_2 ); 96 if( !mixer ) 97 { 98 fprintf(stderr, "al_create_mixer failed\n"); 99 goto error; 100 } 101 al_attach_mixer_to_voice( mixer, voice ); 102 103 /* Load note samples: */ 104 for( i=0, l=notes_length; i<l; i++ ) 105 { 106 char filename[22] = "media/Piano.ff.A3.ogg"; 107 filename[15] = 'A' + i; 108 if (filename[15] > 'B') 109 { 110 filename[16] = '4'; 111 } 112 note_s[ i ] = al_load_sample( filename ); 113 if( note_s[ i ] == NULL ) 114 { 115 fprintf(stderr, "al_load_sample %s failed\n", filename); 116 goto error; 117 } 118 119 /* Create our sample instances: */ 120 note_si[ i ] = al_create_sample_instance( note_s[ i ] ); 121 if( note_si[ i ] == NULL ) 122 { 123 fprintf( stderr, "al_create_sample_instance %s failed\n", filename ); 124 goto error; 125 } 126 /* And attach them to the mixer: */ 127 al_attach_sample_instance_to_mixer( note_si[ i ], mixer ); 128 } 129 130 musicals = al_load_ttf_font("musicals.ttf", 72, 0); 131 if (musicals == NULL) 132 { 133 fprintf(stderr, "al_load_ttf_font failed\n"); 134 goto error; 135 } 136 al_register_event_source(event_queue, al_get_display_event_source(display)); 137 al_register_event_source(event_queue, al_get_keyboard_event_source()); 138 black = al_map_rgb(0, 0, 0); 139 white = al_map_rgb(255, 255, 255); 140 al_init_timeout( &timeout, 0.06); 141 142 while (1) 143 { 144 int w = al_get_display_width(display); 145 int h = al_get_display_height(display); 146 ALLEGRO_EVENT ev; 147 if (al_wait_for_event_until(event_queue, &ev, &timeout) ) 148 { 149 switch (ev.type) 150 { 151 case ALLEGRO_EVENT_DISPLAY_CLOSE: 152 goto clean; 153 break; 154 case ALLEGRO_EVENT_KEY_DOWN: 155 if (ev.keyboard.keycode >= ALLEGRO_KEY_A && ev.keyboard.keycode <= ALLEGRO_KEY_G) 156 { 157 i = ev.keyboard.keycode - ALLEGRO_KEY_A; 158 tmp[ i ] = abcdefg[ i ]; 159 160 play_note( note_si[ i ] ); 161 } 162 else 163 { 164 switch (ev.keyboard.keycode) 165 { 166 case ALLEGRO_KEY_ESCAPE: 167 case ALLEGRO_KEY_Q: 168 goto clean; 169 } 170 } 171 break; 172 case ALLEGRO_EVENT_KEY_UP: 173 if (ev.keyboard.keycode >= ALLEGRO_KEY_A && ev.keyboard.keycode <= ALLEGRO_KEY_G) 174 { 175 i = ev.keyboard.keycode - ALLEGRO_KEY_A; 176 177 tmp[ i ] = ' ' ; 178 /* 179 Uncomment to make the sample instance stop playing on 180 key up event. Does not sound good to me: 181 */ 182 /* 183 al_set_sample_instance_playing( note_si[ i ], false ); 184 */ 185 } 186 break; 187 } 188 } 189 al_clear_to_color(black); 190 191 say_center(w / 2.0, h / 2.0, "abcdefg ABCDEFG"); 192 say_center(w / 2.0, h / 2.0 + 100, tmp); 193 194 al_flip_display(); 195 } 196clean: 197 for ( i=0, l=notes_length; i<l; i++ ) 198 { 199 if( note_si[ i ] ) 200 al_destroy_sample_instance( note_si[ i ] ); 201 if( note_s[ i ] ) 202 al_destroy_sample( note_s[ i ] ); 203 } 204 205 if (musicals) al_destroy_font(musicals); 206 207 if( mixer ) al_destroy_mixer( mixer ); 208 if( voice ) al_destroy_voice( voice ); 209 210 if (event_queue) al_destroy_event_queue(event_queue); 211 if (display) al_destroy_display(display); 212 return status; 213error: 214 status = 1; 215 goto clean; 216}

bamccaig
Member #7,536
July 2006
avatar

Thanks. I think Edgar and I already accomplished that. What remains is for me to test whether it's the data files or the driver causing my issue. I haven't had time to do that yet.. So bump..

Niunio
Member #1,975
March 2002
avatar

Sorry for the late answer but:

This is why nothing ever gets done. I hope somebody updated the docs for me...

You're right, but I'm Spanish and English is quite bad when I try to explain complex things (it really is); that's why I don do it.

Anyway I have to read again this thread carefully to see if I learn more.

-----------------
Current projects: Allegro.pas | MinGRo

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Eric Johnson
Member #14,841
January 2013
avatar

Thanks for the bump. It's a cool little piano program. I'm on Linux, so I had to run it in Wine. It's probably just a Wine issue, but it took about a second between the time I pressed a key and the time the sound began. I'll try it in Windows when I get the chance.

bamccaig
Member #7,536
July 2006
avatar

Eric, you could also just try building the program yourself in Linux. :) I would appreciate somebody else testing in Linux anyway to report their results.

Eric Johnson
Member #14,841
January 2013
avatar

Done. I built it from Piano.zip, but nothing happened when I pressed the keys. Then I tried Izual's edit and it worked. It works flawlessly without delay, so the audio issues were Wine's fault.

AramisL
Member #16,723
August 2017
avatar

Hi bamccaig, as Edgar mentioned, as my little doubt too, have you noticed that the keyboards have hardware limitations?

I mean, it depends on the builder of your keyboard HOW MANY buttons can be pressed at a time, it is called buffer keyboard and this is related with memory and bus in the hardware (and I dont know if OS too, that's why audio cards does exists, aren't they?)

my little grand of sand ;D

bamccaig
Member #7,536
July 2006
avatar

I am using a DAS KEYBOARD so my keyboard should support N-key input. :) But yes, for other keyboards it could certainly be a limitation. As far as "tracker" goes, it's just an experiment to see what I can accomplish, and gain some experience with Allegro audio. And if something fun for me comes out of it even better. I'm already surprised with the little bit of attention it has gotten so perhaps a featureful program would even attract other attention.

Izual
Member #2,756
September 2002
avatar

@Eric: The delay in the sound is propably not Wines fault. The samples contain about a second of silence at the start.

This line in my edit: al_set_sample_instance_position( si, 32000 ); skips about a second of that silence and makes it sounds about right.

And one more thing. When you use al_set_sample_instance_playing( sample_instance, true ); more then once on one sample instance, it will not start playing the sample form the start. I will just keep playing.
To achieve the effect of playing the sample again you either stop the sample first or just set the sample position to desired location. Like in my bam's piano edit.

The sample instance position is reset to the start ( 0 ) when it is finished playing or when it is stopped.

Eric Johnson
Member #14,841
January 2013
avatar

Izual said:

@Eric: The delay in the sound is propably not Wines fault. The samples contain about a second of silence at the start.

Oh that's it then. I do know I've had audio issues with Wine in the past though.

GullRaDriel
Member #3,861
September 2003
avatar

Bump 8-)

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Michael Weiss
Member #223
April 2000

Just thought I'd add my two cents here.

I have used the old allegro 4 sound routine and have just ported to the new
allegro 5 ones.

I had no problems, except for a small misunderstanding when I tried to hook
two mixers up to a voice and only one would work. After troubleshooting and
RTFMing some more I found by design you are only supposed to connect one mixer
to a voice.

So now a have voice -> mixer -> two other mixers.

One of them I have connected to all my sound effects and the other one is
for the background music.

I personally do not think the api is overly complicated. I read the comments
from Edgar asking if anyone had any specific suggestions on how the api or
documentation could be improved, I thought about it, and it seems like
exactly what is needed to get the job done.

Just my two cents worth...

 1   2   3   4 


Go to: