Skip to content

Commit

Permalink
Make Metronome work
Browse files Browse the repository at this point in the history
  • Loading branch information
SubhadeepJasu committed Jul 11, 2021
1 parent 4121e17 commit e59017b
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .buildconfig
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[default]
name=Default
[meson]
name=Meson
runtime=host
config-opts=
run-opts=
prefix=/home/subhadeep/.cache/gnome-builder/install/.vscode/host
prefix=/usr
app-id=
postbuild=
prebuild=
Expand Down
40 changes: 40 additions & 0 deletions src/Core/MetronomeLFOPlayer.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace Ensembles.Core {
public class MetronomeLFOPlayer {
string _lfo_directory_location;
int _time_signature_n;
int _time_signature_d;
string _lfo_file_location;
int _tempo = 120;
public MetronomeLFOPlayer (string lfo_directory_location) {
_lfo_directory_location = lfo_directory_location;
metronome_lfo_player_init ();
}
~MetronomeLFOPlayer () {
metronome_lfo_player_destruct ();
}

public void play_measure (int time_signature_n, int time_signature_d) {
if (_time_signature_n != time_signature_n || _time_signature_d != time_signature_d) {
_time_signature_n = time_signature_n;
_time_signature_d = time_signature_d;
_lfo_file_location = _lfo_directory_location + "/" +
time_signature_n.to_string () + "_" +
time_signature_d.to_string () + ".mtlfo";
metronome_lfo_player_change_base (_lfo_file_location, _tempo, 1920);
} else {
metronome_lfo_player_play ();
}
}

public void set_tempo (int tempo) {
_tempo = tempo;
metronome_lfo_player_set_tempo (_tempo);
}
}
}

extern void metronome_lfo_player_init ();
extern void metronome_lfo_player_destruct ();
extern void metronome_lfo_player_change_base (string mid_file, int tempo, int eol);
extern void metronome_lfo_player_play ();
extern void metronome_lfo_player_set_tempo (int tempo);
2 changes: 1 addition & 1 deletion src/Core/central_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#ifndef CENTRAL_BUS_H
#define CENTRAL_BUS_H

/** Central Clock becomes 1 every time a beat starts and then it goes to 0
/** Central Clock becomes 1 every time a measure starts and then it goes to 0
*/
/** This function gives you the current value of the clock
*/
Expand Down
72 changes: 62 additions & 10 deletions src/Core/metronome_lfo_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,40 @@ fluid_player_t* lfo_player;

int lfo_measure_length = 0;
int lfo_looping = 0;
int lfo_end_of_line = 0;

char* metronome_file_path;

int
lfo_parse_midi_events (void *data, fluid_midi_event_t *event) {
fluid_midi_event_t* new_event = new_fluid_midi_event ();

fluid_midi_event_set_channel (new_event, fluid_midi_event_get_channel (event));
fluid_midi_event_set_control (new_event, fluid_midi_event_get_control (event));
fluid_midi_event_set_program (new_event, fluid_midi_event_get_program (event));
fluid_midi_event_set_value (new_event, fluid_midi_event_get_value (event));
fluid_midi_event_set_velocity (new_event, 105);
fluid_midi_event_set_type (new_event, fluid_midi_event_get_type (event));

int type = fluid_midi_event_get_type (event);
int channel = fluid_midi_event_get_channel (event);
int key = fluid_midi_event_get_key (event);

if (channel == 9) {
// Send data to synth
synthesizer_send_notes_metronome (key, type);
}
return 0;
}


int
lfo_parse_ticks (void* data, int ticks) {
if (ticks >= lfo_end_of_line) {
fluid_player_stop (lfo_player);
}
}


void
metronome_lfo_player_init () {
Expand All @@ -47,28 +78,49 @@ metronome_lfo_player_init () {
}


GThreadFunc
queue_lfo_file_change (int tempo) {
printf("changing...to %s\n", metronome_file_path);
}
void
metronome_lfo_player_change_time_signature (int tempo, float time_signature) {

metronome_lfo_player_change_base (const char* mid_file, int tempo, int eol) {
lfo_end_of_line = eol;
if (lfo_player) {
fluid_player_stop (lfo_player);
delete_fluid_player(lfo_player);
}
lfo_player = new_fluid_player(lfo_synth);
fluid_player_set_playback_callback(lfo_player, lfo_parse_midi_events, lfo_synth);
fluid_player_set_tick_callback (lfo_player, lfo_parse_ticks, lfo_synth);
if (fluid_is_midifile(mid_file)) {
fluid_player_add(lfo_player, mid_file);
}
fluid_player_set_tempo (lfo_player, FLUID_PLAYER_TEMPO_EXTERNAL_BPM, (double)tempo);
fluid_player_play (lfo_player);
printf ("d:\n");
}

void
metronome_lfo_player_destruct () {
/* wait for playback termination */
fluid_player_stop (lfo_player);
fluid_player_join(lfo_player);
if (lfo_player) {
fluid_player_stop (lfo_player);
fluid_player_join(lfo_player);
delete_fluid_player(lfo_player);
lfo_player = NULL;
}
/* cleanup */
delete_fluid_audio_driver(lfo_adriver);
delete_fluid_player(lfo_player);
delete_fluid_synth(lfo_synth);
delete_fluid_settings(lfo_settings);
}

void
metronome_lfo_player_loop () {
metronome_lfo_player_play () {
fluid_player_stop (lfo_player);
fluid_player_seek (lfo_player, 0);
fluid_player_play (lfo_player);
}

void
metronome_lfo_player_set_tempo (int tempo) {
if (lfo_player) {
fluid_player_set_tempo (lfo_player, FLUID_PLAYER_TEMPO_EXTERNAL_BPM, (double)tempo);
}
}
14 changes: 10 additions & 4 deletions src/Core/style_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,10 @@ queue_style_file_change (int use_previous_tempo) {
}
if (player) {
printf ("b:\n");
fluid_player_stop (player);
fluid_player_join(player);
delete_fluid_player(player);
player = NULL;
printf ("c:\n");
}
player = new_fluid_player(synth);
Expand Down Expand Up @@ -412,12 +415,15 @@ style_player_reload_style () {

void
style_player_destruct () {
/* wait for playback termination */
fluid_player_stop (player);
fluid_player_join(player);
/* cleanup */
delete_fluid_audio_driver(adriver);
delete_fluid_player(player);
if (player) {
/* wait for playback termination */
fluid_player_stop (player);
fluid_player_join(player);
delete_fluid_player(player);
player = NULL;
}
delete_fluid_synth(synth);
delete_fluid_settings(settings);
}
Expand Down
19 changes: 18 additions & 1 deletion src/Core/synthesizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,14 @@ synthesizer_set_defaults () {
fluid_synth_cc (realtime_synth, 1, 74, 0);
fluid_synth_cc (realtime_synth, 2, 74, 0);

// Reverb and Chorus ro R1 voice
// Reverb and Chorus for R1 voice
fluid_synth_cc (realtime_synth, 0, 91, 4);
fluid_synth_cc (realtime_synth, 0, 93, 1);

// Reverb and Chorus for Metronome
fluid_synth_cc (realtime_synth, 9, 91, 0);
fluid_synth_cc (realtime_synth, 9, 93, 0);

// Default gain for Realtime synth
fluid_synth_cc (realtime_synth, 0, 7, 100);
fluid_synth_cc (realtime_synth, 1, 7, 90);
Expand Down Expand Up @@ -197,6 +201,9 @@ synthesizer_init (const gchar* loc) {
fluid_synth_program_select (realtime_synth, 3, realtime_synth_sf_id, 0, 5);
fluid_synth_program_select (realtime_synth, 4, realtime_synth_sf_id, 0, 33);
fluid_synth_program_select (realtime_synth, 5, realtime_synth_sf_id, 0, 49);

// Initialize metronome voice
fluid_synth_program_select (realtime_synth, 9, realtime_synth_sf_id, 128, 0);
}
fx_init ();
style_adriver = new_fluid_audio_driver2(style_synth_settings, fx_function, (void *) &fx_data);
Expand Down Expand Up @@ -320,6 +327,15 @@ handle_events_for_styles (fluid_midi_event_t *event) {
return fluid_synth_handle_midi_event(style_synth, event);
}

int
synthesizer_send_notes_metronome (int key, int on) {
if (on == 144) {
fluid_synth_noteon (realtime_synth, 9, key, 127);
} else {
fluid_synth_noteoff (realtime_synth, 9, key);
}
}

int
synthesizer_send_notes (int key, int on, int velocity, int* type) {
if (get_central_accompaniment_mode () > 0) {
Expand Down Expand Up @@ -362,6 +378,7 @@ synthesizer_send_notes (int key, int on, int velocity, int* type) {
if (on == 144) {
fluid_synth_noteon (realtime_synth, 0, key + ((synthesizer_octave_shifted > 0) ? (synthesizer_octave * 12) : 0) + ((synthesizer_transpose_enable > 0) ? synthesizer_transpose : 0), velocity);
voice_velocity_buffer[0] = velocity;
printf("%d\n", key);
} else if (on == 128) {
fluid_synth_noteoff (realtime_synth, 0, key + ((synthesizer_octave_shifted > 0) ? (synthesizer_octave * 12) : 0) + ((synthesizer_transpose_enable > 0) ? synthesizer_transpose : 0));
voice_velocity_buffer[0] = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/Core/synthesizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
int handle_events_for_styles (fluid_midi_event_t *event);

void synthesizer_send_notes_metronome (int key, int on);

/** This function is used to stop all synthesizer sounds for styles
* except channel 10 i.e. drums
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Shell/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ namespace Ensembles.Shell {
Ensembles.Core.Synthesizer synthesizer;
Ensembles.Core.StyleDiscovery style_discovery;
Ensembles.Core.StylePlayer style_player;
Ensembles.Core.MetronomeLFOPlayer metronome_player;
Ensembles.Core.CentralBus bus;
Ensembles.Core.Controller controller_connection;

string sf_loc = Constants.SF2DATADIR + "/EnsemblesGM.sf2";
string sf_schema_loc = Constants.SF2DATADIR + "/EnsemblesGMSchema.csv";
string metronome_lfo_directory = Constants.PKGDATADIR + "/MetronomesAndLFO";
public MainWindow () {
Gtk.Settings settings = Gtk.Settings.get_default ();
settings.gtk_application_prefer_dark_theme = true;
Expand Down Expand Up @@ -125,6 +127,8 @@ namespace Ensembles.Shell {
);
});

metronome_player = new Ensembles.Core.MetronomeLFOPlayer (metronome_lfo_directory);

make_ui_events ();

load_voices ();
Expand All @@ -134,6 +138,7 @@ namespace Ensembles.Shell {
beat_counter_panel.sync ();
style_controller_view.sync ();
main_display_unit.set_measure_display (Ensembles.Core.CentralBus.get_measure ());
metronome_player.play_measure (4, 4);
});
bus.system_halt.connect (() => {
style_player.reload_style ();
Expand All @@ -149,6 +154,7 @@ namespace Ensembles.Shell {
bus.loaded_tempo_change.connect ((tempo) => {
beat_counter_panel.change_tempo (tempo);
main_display_unit.set_tempo_display (tempo);
metronome_player.set_tempo (tempo);
});
bus.split_key_change.connect (() => {
main_keyboard.update_split ();
Expand Down
2 changes: 2 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ensembles_sources_vala = files (
'Core/StyleDiscovery.vala',
'Core/CentralBus.vala',
'Core/StylePlayer.vala',
'Core/MetronomeLFOPlayer.vala',
'Core/StyleAnalyser.vala',
'Core/Voice.vala',
'Core/VoiceAnalyser.vala'
Expand All @@ -58,6 +59,7 @@ ensembles_sources_c = files (
'Core/synthesizer.c',
'Core/controller.c',
'Core/style_player.c',
'Core/metronome_lfo_player.c',
'Core/style_analyser.c',
'Core/chord_finder.c',
'Core/voice_analyser.c'
Expand Down

0 comments on commit e59017b

Please sign in to comment.