Skip to content

Commit

Permalink
Add speed scale parameter to midi player, resolves #18
Browse files Browse the repository at this point in the history
  • Loading branch information
nlaha committed Apr 17, 2024
1 parent 249e34a commit a45d440
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
6 changes: 5 additions & 1 deletion extension/src/midi_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ MidiPlayer::MidiPlayer()
{
// initialize variables
this->current_time = 0;
this->speed_scale = 1;
this->loop = false;
this->state = PlayerState::Stopped;
this->manual_process = false;

set_process_thread_group(ProcessThreadGroup::PROCESS_THREAD_GROUP_MAIN_THREAD);
}
Expand Down Expand Up @@ -126,7 +130,7 @@ void MidiPlayer::process_delta(double delta)

// increment time, current time will hold the
// number of seconds since starting
this->current_time += delta;
this->current_time += delta * speed_scale;
}
}
}
32 changes: 30 additions & 2 deletions extension/src/midi_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ class MidiPlayer : public Node
ClassDB::bind_method(D_METHOD("get_manual_process"), &MidiPlayer::get_manual_process);
ClassDB::bind_method(D_METHOD("set_manual_process", "manual_process"), &MidiPlayer::set_manual_process);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "manual_process"), "set_manual_process", "get_manual_process");


ClassDB::bind_method(D_METHOD("get_speed_scale"), &MidiPlayer::get_speed_scale);
ClassDB::bind_method(D_METHOD("set_speed_scale", "speed_scale"), &MidiPlayer::set_speed_scale);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale"), "set_speed_scale", "get_speed_scale");

ClassDB::bind_method(D_METHOD("process_delta", "delta"), &MidiPlayer::process_delta);

ADD_SIGNAL(MethodInfo("finished"));
Expand All @@ -64,14 +68,27 @@ class MidiPlayer : public Node
};

private:
/// @brief The midi resource to play
Ref<MidiResource> midi;

/// @brief The current state of the player
PlayerState state;

/// @brief The current time in seconds
double current_time;

/// @brief The current track index offsets
Array track_index_offsets;

/// @brief Whether to loop the midi playback
bool loop;

/// @brief Whether to manually process the midi playback
bool manual_process;

/// @brief The speed scale of the midi playback (1.0 = normal speed, 2.0 = double speed, 0.5 = half speed, etc.)
double speed_scale;

public:
virtual void _process(double delta) override;
void process_delta(double delta);
Expand All @@ -83,6 +100,11 @@ class MidiPlayer : public Node
void stop();
void pause();

double get_speed_scale()
{
return this->speed_scale;
};

bool get_manual_process()
{
return this->manual_process;
Expand All @@ -103,6 +125,11 @@ class MidiPlayer : public Node
return this->current_time;
};

void set_speed_scale(double speed_scale)
{
this->speed_scale = speed_scale;
};

void set_manual_process(bool manual_process)
{
this->manual_process = manual_process;
Expand Down Expand Up @@ -133,7 +160,8 @@ class MidiPlayer : public Node
if (this->current_time >= event_time)
{
this->track_index_offsets[i] = j;
} else
}
else
{
break;
}
Expand Down
1 change: 0 additions & 1 deletion game/mii_channel.mid.import
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ dest_files=["res://.godot/imported/mii_channel.mid-65a1f1ae83fe8e619f2b937cc77ff

[params]

only_notes=false
1 change: 0 additions & 1 deletion game/zeal.mid.import
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ dest_files=["res://.godot/imported/zeal.mid-c2e1a4e743859c61aef9a8e99052386a.res

[params]

only_notes=false

0 comments on commit a45d440

Please sign in to comment.