[A4/A5] Homemade Audio Primer
Chris Katko

I need to take an audio sample (WAV/MP3/OGG/I don't care). Apply some manual processing to the sample, and then play it.

Simple enough, but I have no experience with programming audio. Is A4 or A5 easier, and is there a straight-forward example for doing that?

I looked through the A5 examples and they all seem to be way more complex than necessary.

I also intend to switch playback at times between the original file, and resampled ones. Preferably with seamless transitions.

[edit] If it helps, the boundaries of the transitions aren't going to be rapid, and can have wiggle room to line up with audio buffers. I only need a response time of around a second for switching, but I absolutely need them to line up. Where one ends, the other should begin, as if they were the same wave. (Imagine switching between two identical waveforms, there should be no glitch or offset.)

Thomas Fjellstrom

You'll need streams for that. Both A5 and A4 support it. A4 might be simpler for manual processing, as it doesn't have the more verbose (and featureful) api. Though I think A5 mixers can do resampling for you (assuming you're talking about up/down sampling between audio frequencies ie: 44100<>48000 etc).

SiegeLord

If your manual processing is fast enough to be done in real time, you could perform the seamless transition inside your post-processing function. It'd be something like this:

#SelectExpand
1al_reserve_samples(0); 2 3auto sample = al_load_sample("..."); 4auto instance = al_create_sample_instance(sample); 5 6auto pp_mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); 7al_set_mixer_postprocess_callback(pp_mixer, post_process, post_process_data); 8 9al_attach_sample_instance_to_mixer(instance, pp_mixer); 10al_attach_mixer_to_mixer(pp_mixer, al_get_default_mixer()); 11 12al_play_sample_instance(instance); 13 14// later 15 16post_process_data->set_mixing_factor = 0.5;

Alternatively, you could create a second instance of that sample, attach it to the default mixer and then adjust the volumes of the two instances... I'm not sure it'll be easy to line them up perfectly that way, however.

pkrcel

Aligning two different sample instances (which are the same as streams) MIGHT prove to be nigh impossible or close to it, but you know my udnerstangin of Allegro is still quite limited.

I guess that the postprocessing callback it's the most sensible idea, given that you can "tone" the mixing factor as in the pseudocode example Siegelord has given.

If you're only about resampling at a different rate, ALLEGRO_MIXER do that for your decimating/interpolating the samples with linear/cubic/etc interpolation... at your whim (thou "default" quality of mixer interpolation has to be set at build time IIRC).

EDIT: While we're at it, I think Suegelord suggested the Default Mixer since that is automagically attached at a working ALLEGRO_VOICE but I'm not sure its the way to go if you want to be sure about the output bitrate yourself. I might be wrong thou.

Chris Katko
SiegeLord said:

If your manual processing is fast enough to be done in real time, you could perform the seamless transition inside your post-processing function.

That's a good point. In fact, the sampling I'm doing can be entirely done with the main sample, or working copy of it. That is, I don't really need transitions in real-time, as long as my processing function can do it before hand. So I can copy the main file, do my "changes" and then write straight into it. There's no worry about lining up different samples.

That simplifies things greatly unless I decide to do something real-time.

Though I think A5 mixers can do resampling for you (assuming you're talking about up/down sampling between audio frequencies ie: 44100<>48000 etc).

I'm playing around with audio analysis and other ideas I've got randomly around in my brain. But the core of it is, I need to have my own control over the transfer function.

For example, "cut the bit rate in half for a portion of the sample" and then "write that data back over the original waveform buffer for that same interval." Now that can probably be done with A5's internal resampling, but I want more control and more options for distortion and modification in addition to that. Such as converting the sample to FFT (powers of sin/cos), and keep only X terms (effectively a low pass).

Thread #615041. Printed from Allegro.cc