I want an example where load a wav file and play it, and when it finished sound start again and again and again...
I want to use the function play_audio_stream, but I dont know how.
I try these, but not soud anything:
int main()
{
allegro_init();
install_keyboard();
install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
//create the audio stream
AUDIOSTREAM *stream = play_audio_stream(1024, 8, FALSE, 22050, 255, 128);
//Them create the sample "archivo" loading a wav
SAMPLE *archivo=load_wav("wav.wav");
while(!key[KEY_ESC]){
void *mem_chunk;
mem_chunk = get_audio_stream_buffer(stream);
//when the sound buffer is void:
if (mem_chunk != NULL){
//Put into the buffer the sample "archivo"
mem_chunk=&archivo;
//Indicate that the buffer is ready
free_audio_stream_buffer(stream);
}
}
}
I am from uruguay and my english is basic, please write correctly
This may or may not help you but I wrote a basic class that loads, plays and stops audio using ALLEGRO_SAMPLE.
Hope this helps getting sound to work.
Append: I haven't tested this code myself since I am now going to bed. Must be up for the kids in the morning.
I want do it want do it with play_audio_stream
my reason:
"The audio stream functions are for playing digital sounds that are too big to fit in a regular SAMPLE structure, either because they are huge files that you want to load in pieces as the data is required, or because you are doing something clever like generating the waveform on the fly."
http://alleg.sourceforge.net/latestdocs/en/alleg028.html
I never read that I shall have to add it to my class at some point for music files
The line 'mem_chunk = &archivo;' does nothing. You have to fill the buffer using the pointer provided to you by get_audio_stream_buffer.
Example :
If you're going to use SAMPLE*s, then use the routines for playing samples - play_sample.
it give me an error:
`void*' is not a pointer-to-object type
I put 3 lines to your example to generate my data:
#include <allegro.h>
#include <math.h>
int main()
{
allegro_init();
install_keyboard();
install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
int length = 1024;
int bits = 8;
int stereo = 0;
int freq = 22050;
int vol = 255;
int pan = 127;
//I generate my data
float PI=3.14159;
char data[1024];
for(int i=0;i<1024;i++) data[i]=sin(i*2*PI/10)*127;//I create the wave, 1 cycle per 10 sampes
AUDIOSTREAM *stream = play_audio_stream(1024, 8, FALSE, 22050, 255, 128);
void* mem_chunk;
mem_chunk = get_audio_stream_buffer(stream);
int bytes = length * (bits / 8) * (stereo ? 2 : 1);
if (mem_chunk) {
for (int i = 0 ; i < bytes ; ++i) {
mem_chunk[i] = data[i];
}
}
}
thank you for the reply
`void*' is not a pointer-to-object type
...
void* mem_chunk; mem_chunk = get_audio_stream_buffer(stream); int bytes = length * (bits / 8) * (stereo ? 2 : 1); if (mem_chunk) { for (int i = 0 ; i < bytes ; ++i) { mem_chunk[i] = data[i];
Sorry, I mislead you. Before you can assign values to the array returned by get_audio_stream_buffer, you must cast it to a specific type. Since you are using a 8 bit mono stream, then you want to cast it to an unsigned char*.
The sample data are always in unsigned format.
This means that you need to convert your data from signed format into unsigned format.
So, before you had this :
char data[1024]; for(int i=0;i<1024;i++) data[i]=sin(i*2*PI/10)*127;//I create the wave, 1 cycle per 10 sampes
But now you should have this :
unsigned char data[1024]; for (int i = 0 ; i < 1024 ; ++i) { float raw = sin((float)i*2.0f*M_PI/10.0); unsigned char d = (unsigned char)((raw*127.5f) + 127.5f); data[i] = d; }
Edit
Please use <code>code goes here...</code> tags when posting code. It makes it much easier to read.
ok thank
I want to know if allegro in the code use 1 or 2 buffers for the sound???
There are two buffers for the AUDIOSTREAM :
You can think of an AUDIOSTREAM structure as a wrapper around two audio buffers. The first thing you do is fill both buffers with sound data and let Allegro play them. Once the first buffer has been played, the second starts, and Allegro lets you know you have to fill the other one (i.e. graphics double buffering applied to sounds too big to fit into memory).
So fill both when you start the stream, and then only fill the other buffer when get_audio_stream_buffer returns non-null.
Can I use more than 2 buffers?
I want to make a piano, and I am thinking use 1 buffer per musical note, beacause of these way I should not to refill the buffers and is would be more efficient and fast. I only would need to indicate what buffer play.
Beacuse I made a litle program that play a sound with AUDIOSTREAM, and when I order to change the sound, the program takes too long (you know that the speed is very important in a piano)
Can I use more than 2 buffers?
Not with an AUDIOSTREAM, no. You can make your own 'buffers' and then copy them into your AUDIOSTREAM buffer though.
I want to make a piano, and I am thinking use 1 buffer per musical note, beacause of these way I should not to refill the buffers and is would be more efficient and fast. I only would need to indicate what buffer play.
You could make your own SAMPLE's, modifying the SAMPLE::data array after making a new sample with create_sample. Then you could just use play_sample.
Beacuse I made a litle program that play a sound with AUDIOSTREAM, and when I order to change the sound, the program takes too long (you know that the speed is very important in a piano)
//AUDIOSTREAM *play_audio_stream(int len, int bits, int stereo, int freq, int vol, int pan); AUDIOSTREAM *stream = play_audio_stream(1024, 8, FALSE, 22050, 255, 128);
The reason it takes so long to hear the sound again is because you are not filling the buffer fast enough. With only 1024 frames in the buffer at 22050 frames per second, you need to refill the buffer (22050/1024 ~= 21) 21 times every second. You can either use a larger number of frames (len) and refill it less often (which will make the sound take longer to change), or you can continue to use a small number of frames and update the sound more often.
I taste what you said, but it doesn't change.
Look at these example, I can set the variable whit values bigers or smollers than 1024, but there arent change.
The example has to play the note1 or note2 if the key z is presed or not
what is the better way to get a fast reply of the sound about the keyboard?
I compiled your program and ran it, and there is a noticeable delay when changing sounds.
I think this has to do with Allegro's internal mixer buffer size, and I don't believe that can be changed by the user. Sorry.
play_sample is much faster for me, and plays pretty much immediately after a key press for me.