Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to find the last possible position of a sample instance

Credits go to Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
How to find the last possible position of a sample instance
AgentBaron
Member #16,630
January 2017

Is it possible to find the last possible position of a sample instance?

My brute force method is to have the program output al_get_sample_instance_position(instance) every tick and then listen to the instance to completion, however I can't help but feel like there is a more elegant way to get this in the code. Is there?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

AgentBaron
Member #16,630
January 2017

al_get_sample_instance_length() is actually tremendously helpful, thanks! What's weird, though, is that when I use my brute force method and compare it to al_get_sample_instance_position(), I get a value that is less than my brute force output.

I'm trying to get a song to loop back on itself, but it has an intro. So I'm trying to play the intro and then get the main looping part of the song to play immediately after, seamlessly. The problem is that I am not aware of a way to track when the song is finished playing to tell this thing to loop, and I thought that if I could know the length I could nail it that way. This works somewhat, except that I am getting different values when I play the song. I can't begin to postulate what this means, but it does create a conundrum for me.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Bruce Perry
Member #270
April 2000

You could use an audio stream and feed the sample data to it manually, thereby eliminating the glitch? It might not lead to the best performance possible, but it will sound perfect.

Trying to use CPU timing or system clock timing to place an audio sample will always be imperfect. It's much better if you can specify the exact position in the output stream that the sample should start (which you can do if you're feeding the data to a stream yourself).

Or does Allegro have a 'play something at a scheduled instant in the audio output' feature? Could one be added?

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Polybios
Member #12,293
October 2010

Maybe you could also use al_set_audio_stream_loop_secs directly to set the stream's loop points, if you use audio streams via al_load_audio_stream()

AgentBaron
Member #16,630
January 2017

Edgar's idea worked perfectly for what I was trying to do. I should have known better, but for some reason I was under the impression that the playing property wouldn't get kicked to false once it stopped playing.

Monitoring that and throwing a bool around helped me get everything taken care of. Thanks so much for the help!

For the sake of time, this seems like the best option, and then once I no longer have due dates on this project, I can experiment with some of the other ideas you all have thrown around to see what I can do about optimization.

Go to: