mirror of
https://github.com/ZDoom/fluidsynth.git
synced 2024-11-10 15:01:40 +00:00
0d98c47545
Proposing a new event queue for the sequencer, based on prior discussion: https://lists.nongnu.org/archive/html/fluid-dev/2019-12/msg00001.html With this change fluidsynth will require a C++98 compliant compiler. Consider this as RFC, feedback is welcome. The "pain-points" from the discussion: #### 1. It is slow. Done (thanks to heap sort), see runtime of `test_seq_event_queue_sort`. #### 2. A meaningful ordering for events with the same tick has not been considered. Done, see comments in `fluid_seq_queue.cpp`. #### 3. Complicated implementation Now uses one single event queue, which requires C++98. Implemented similarly to std::priority_queue by using heap sort. The "queue" I use currently is of type `std::deque`. `deque` does not provide preallocation. `std::vector` does provide it. However, `std::deque` has the huge advantage that appending additional elements is cheap. For `std::vector` appending new elements would require to reallocate all the memory and copy it to the new array. So, * either use `std::deque` with the risk that memory allocation may occur during `fluid_sequencer_send_at()`, or * use `std::vector` with a preallocated pool of events and make `fluid_sequencer_send_at()` when the `vector` runs out of capacity. Comments? #### 4. Events that have been processed are deleted and gone. After having thought about this more, this is the correct behavior. After events have been dispatched, they must be released to free underlying memory, see point 3. For the very rare case that a client (e.g. fluid_player) may require those events in the future, the client should be responsible for storing the events somewhere. #### 5. The sequencer supports the system timer as alternative time source. The conclusion from the mailing list was that the system timer can be removed. This has been done. #### 6. Time Scaling Time scaling can now be used for arbitrary tempo changes. The previous implementation was capable of that as well, however, the time-scale was limited to 1000. The only limitation for the scale is now >0, see `test_seq_scale`. ### Other Points * `fluid_sequencer_remove_events()` turned out to be broken before, as it did not remove all events from the queue. This has been fixed, see `test_seq_event_queue_remove`. * Consider the following code executed by `threadA`: ```c fluid_sequencer_send_at(event0); fluid_sequencer_set_time_scale(); // new scale fluid_sequencer_send_at(event1); ``` The new scale will be definitely applied to `event1`. However, if another concurrently running `threadB` executes `fluid_sequencer_process()`, it was previously not clear, whether the new scale was also applied to event0. This depends on whether `event0` was still in the `preQueue`, and this depends on `event0.time` and the tick count that `fluid_sequencer_process()` is being called with. This has been changed. As of now, events are queued with their timestamp AS-IS. And only the latest call to `fluid_sequencer_set_time_scale()` is being considered during `fluid_sequencer_process()`. This makes the implementation very simple, i.e. no events need to be changed and the sequencer doesn't have to be locked down. On the other hand, it means that `fluid_sequencer_set_time_scale()` can only be used for tempo changes when called from the sequencer callback. In other words, if `threadA` executes the code above followed by `fluid_sequencer_process()`, `event0` and `event1` will be executed with the same tempo, which is the latest scale provided to the seq. Is this acceptable? The old implementation had the same limitation. And when looking through the internet, I only find users who call `fluid_sequencer_set_time_scale()` from the sequencer callback. Still, this is a point I'm raising for discussion.
186 lines
5.3 KiB
C
186 lines
5.3 KiB
C
#include "test.h"
|
|
#include "fluidsynth.h"
|
|
#include "utils/fluid_sys.h"
|
|
#include "synth/fluid_event.h"
|
|
|
|
static fluid_sequencer_t *sequencer;
|
|
static short synthSeqID, mySeqID;
|
|
|
|
static void sendnoteon(int chan, short key, short velocity, unsigned int time)
|
|
{
|
|
fluid_event_t *evt = new_fluid_event();
|
|
fluid_event_set_source(evt, -1);
|
|
fluid_event_set_dest(evt, synthSeqID);
|
|
fluid_event_noteon(evt, chan, key, velocity);
|
|
TEST_SUCCESS(fluid_sequencer_send_at(sequencer, evt, time, 0));
|
|
delete_fluid_event(evt);
|
|
}
|
|
|
|
static void sendpc(int chan, short val, unsigned int time)
|
|
{
|
|
fluid_event_t *evt = new_fluid_event();
|
|
fluid_event_set_source(evt, -1);
|
|
fluid_event_set_dest(evt, synthSeqID);
|
|
fluid_event_program_change(evt, chan, val);
|
|
TEST_SUCCESS(fluid_sequencer_send_at(sequencer, evt, time, 0));
|
|
delete_fluid_event(evt);
|
|
}
|
|
|
|
static void schedule_next_callback(unsigned int time)
|
|
{
|
|
fluid_event_t *evt = new_fluid_event();
|
|
fluid_event_set_source(evt, -1);
|
|
fluid_event_set_dest(evt, mySeqID);
|
|
fluid_event_timer(evt, NULL);
|
|
TEST_SUCCESS(fluid_sequencer_send_at(sequencer, evt, time, 0));
|
|
delete_fluid_event(evt);
|
|
}
|
|
|
|
static const unsigned int expected_callback_times[] =
|
|
{
|
|
0, 0, 120, 120, 240, 240, 360, 360, 480, 720,
|
|
840, 840, 960, 960, 1080, 1080, 1200, 1440,
|
|
1560, 1560, 1680, 1680, 1800, 1800, 1920, 2160,
|
|
2280, 2280, 2400, 2400, 2520, 2520, 2640,
|
|
2880, 3000, 3000, 3120, 3120, 3240, 3240, 3360,
|
|
18080
|
|
};
|
|
|
|
static void fake_synth_callback(unsigned int time, fluid_event_t *event, fluid_sequencer_t *seq, void *data)
|
|
{
|
|
static int callback_idx = 0;
|
|
|
|
TEST_ASSERT(time == expected_callback_times[callback_idx++]);
|
|
FLUID_LOG(FLUID_INFO, "synth callback time: %u %u", time, fluid_event_get_time(event));
|
|
}
|
|
|
|
static void seq_callback(unsigned int time, fluid_event_t *event, fluid_sequencer_t *seq, void *data)
|
|
{
|
|
static int phase = 0;
|
|
|
|
switch(phase)
|
|
{
|
|
case 0:
|
|
sendpc(0, 47, 0);
|
|
fluid_sequencer_set_time_scale(seq, 320);
|
|
sendnoteon(0, 47, 60, 0);
|
|
sendnoteon(0, 47, 0, 120);
|
|
sendnoteon(0, 47, 90, 120);
|
|
sendnoteon(0, 47, 0, 240);
|
|
sendnoteon(0, 47, 110, 240);
|
|
sendnoteon(0, 47, 0, 360);
|
|
sendnoteon(0, 47, 127, 360);
|
|
sendnoteon(0, 47, 0, 480);
|
|
schedule_next_callback(480 + 240);
|
|
break;
|
|
|
|
case 1:
|
|
fluid_sequencer_set_time_scale(seq, 1280 / 2);
|
|
sendnoteon(0, 47, 60, 0);
|
|
sendnoteon(0, 47, 0, 120);
|
|
sendnoteon(0, 47, 80, 120);
|
|
sendnoteon(0, 47, 0, 240);
|
|
sendnoteon(0, 47, 90, 240);
|
|
sendnoteon(0, 47, 0, 360);
|
|
sendnoteon(0, 47, 100, 360);
|
|
sendnoteon(0, 47, 0, 480);
|
|
schedule_next_callback(480 + 240);
|
|
break;
|
|
|
|
case 2:
|
|
fluid_sequencer_set_time_scale(seq, 800);
|
|
sendnoteon(0, 47, 60, 0);
|
|
sendnoteon(0, 47, 0, 120);
|
|
sendnoteon(0, 47, 80, 120);
|
|
sendnoteon(0, 47, 0, 240);
|
|
sendnoteon(0, 47, 90, 240);
|
|
sendnoteon(0, 47, 0, 360);
|
|
sendnoteon(0, 47, 100, 360);
|
|
sendnoteon(0, 47, 0, 480);
|
|
schedule_next_callback(480 + 240);
|
|
break;
|
|
|
|
case 3:
|
|
fluid_sequencer_set_time_scale(seq, 1000);
|
|
sendnoteon(0, 47, 60, 0);
|
|
sendnoteon(0, 47, 0, 120);
|
|
sendnoteon(0, 47, 80, 120);
|
|
sendnoteon(0, 47, 0, 240);
|
|
sendnoteon(0, 47, 90, 240);
|
|
sendnoteon(0, 47, 0, 360);
|
|
sendnoteon(0, 47, 100, 360);
|
|
sendnoteon(0, 47, 0, 480);
|
|
schedule_next_callback(480 + 240);
|
|
break;
|
|
|
|
case 4:
|
|
fluid_sequencer_set_time_scale(seq, 320 / 2);
|
|
sendnoteon(0, 47, 60, 0);
|
|
sendnoteon(0, 47, 0, 120);
|
|
sendnoteon(0, 47, 80, 120);
|
|
sendnoteon(0, 47, 0, 240);
|
|
sendnoteon(0, 47, 90, 240);
|
|
sendnoteon(0, 47, 0, 360);
|
|
sendnoteon(0, 47, 100, 360);
|
|
sendnoteon(0, 47, 0, 480);
|
|
break;
|
|
|
|
case 5:
|
|
// this is the unregistering event, ignore
|
|
break;
|
|
|
|
default:
|
|
TEST_ASSERT(0);
|
|
}
|
|
|
|
phase++;
|
|
}
|
|
|
|
// for debug, uncomment below to hear the notes
|
|
// #define SOUND
|
|
|
|
int main(void)
|
|
{
|
|
int i;
|
|
#ifdef SOUND
|
|
fluid_settings_t *settings = new_fluid_settings();
|
|
fluid_settings_setstr(settings, "audio.driver", "alsa");
|
|
fluid_synth_t *synth = new_fluid_synth(settings);
|
|
TEST_SUCCESS(fluid_synth_sfload(synth, TEST_SOUNDFONT, 1));
|
|
#endif
|
|
|
|
sequencer = new_fluid_sequencer2(0);
|
|
TEST_ASSERT(sequencer != NULL);
|
|
|
|
#ifdef SOUND
|
|
synthSeqID = fluid_sequencer_register_fluidsynth(sequencer, synth);
|
|
#else
|
|
// register fake synth as first destination
|
|
synthSeqID = fluid_sequencer_register_client(sequencer, "fake synth", fake_synth_callback, NULL);
|
|
#endif
|
|
TEST_SUCCESS(synthSeqID);
|
|
// register myself as scheduling destination
|
|
mySeqID = fluid_sequencer_register_client(sequencer, "me", seq_callback, NULL);
|
|
TEST_SUCCESS(mySeqID);
|
|
|
|
// initialize our absolute date
|
|
schedule_next_callback(0);
|
|
|
|
#ifdef SOUND
|
|
fluid_audio_driver_t *adriver = new_fluid_audio_driver(settings, synth);
|
|
fluid_msleep(100000);
|
|
delete_fluid_audio_driver(adriver);
|
|
delete_fluid_synth(synth);
|
|
delete_fluid_settings(settings);
|
|
#else
|
|
|
|
for(i = 0; i < 100000; i++)
|
|
{
|
|
fluid_sequencer_process(sequencer, i);
|
|
}
|
|
|
|
#endif
|
|
|
|
delete_fluid_sequencer(sequencer);
|
|
return EXIT_SUCCESS;
|
|
}
|