Skip to content

Commit

Permalink
[chore] added some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Oct 24, 2024
1 parent b308767 commit c267d2c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions example/midi_play.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:tekartik_midi/midi_player_base.dart';
class _MidiPlayer extends MidiPlayerBase {
@override
void rawPlayEvent(PlayableEvent event) {
// ignore: avoid_print
print(event);
}

Expand Down
7 changes: 7 additions & 0 deletions lib/midi_file_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import 'package:tekartik_common_utils/list_utils.dart';

import 'midi.dart';

/// Playable event
class PlayableEvent {
/// in millis
final num timestamp; // ms
/// The event to play
final MidiEvent midiEvent;

/// Constructor
PlayableEvent(this.timestamp, this.midiEvent);

@override
Expand All @@ -21,10 +25,13 @@ class PlayableEvent {

/// Prepare located events
class LocatedTrackPlayer {
/// The track
MidiTrack track;

/// Constructor
LocatedTrackPlayer(this.track);

/// List of located events
List<LocatedEvent> get preLocatedEvents {
var events = <LocatedEvent>[];
var currentTime = 0;
Expand Down
2 changes: 2 additions & 0 deletions lib/midi_player_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,12 @@ abstract class MidiPlayerBase {
}
}

/// Get the total duration of the file
num get totalDurationMs {
return _midiFilePlayer!.totalDurationMs;
}

/// Get the current position in the file
num get currentAbsoluteMs {
if (!_isPlaying) {
return 0;
Expand Down
17 changes: 14 additions & 3 deletions lib/src/buffer/midi_buffer.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
abstract class Buffer {
abstract class _Buffer {
late List<int> _data;
int _position = 0;

Expand All @@ -19,7 +19,9 @@ abstract class Buffer {
}
}

class InBuffer extends Buffer {
/// Input buffer
class InBuffer extends _Buffer {
/// Constructor
InBuffer(List<int> data) {
_data = data;
}
Expand All @@ -29,37 +31,46 @@ class InBuffer extends Buffer {

//int operator [](int index) => _data[index];

/// Get the next byte and advance the position
int next() {
return _data[_position++];
}

/// Skip [count] bytes
void skip(int count) {
_position += count;
}
}

class OutBuffer extends Buffer {
/// Output buffer
class OutBuffer extends _Buffer {
@override
int get length => _position;

/// Constructor
OutBuffer(int size) {
_data = List<int>.filled(size, 0);
}

/// Restart the buffer
void restart() {
_position = 0;
}

/// Add a byte
void add(int value) {
_data[_position++] = value;
}

/// get the remaining count
int get remainingAvailable => _data.length - _position;

/// Check if there is enough space
bool hasAvailable(int size) {
return remainingAvailable >= size;
}

/// Check if the data is equal to the list
bool equalsList(List<int> data) {
if (length == data.length) {
for (var i = 0; i < length; i++) {
Expand Down
19 changes: 17 additions & 2 deletions lib/src/midi/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TrackEvent {
final int deltaTime;
final MidiEvent midiEvent;

/// Constructor
TrackEvent(this.deltaTime, this.midiEvent);

@override
Expand Down Expand Up @@ -85,32 +86,40 @@ abstract class MidiEvent {
/// Meta command.
static const int cmdMetaEvent = 0xFF;

/// Constructor
MidiEvent();

/// Constructor with command
MidiEvent.withParam(this.command);

/// Compute command from an event type and a channel
static int commandChannel(int eventType, int channel) {
return ((eventType << 4) | (channel & 0xF));
}

/// Get the event type
@Deprecated('use commandGetEventType')
static int commandGetCommand(int command) => commandGetEventType(command);

/// Get the event type
static int commandGetEventType(int command) {
return ((command & 0xF0) >> 4);
}

/// Get the channel
static int commandGetChannel(int command) {
// (command & 0xF)
return (command & 0xF);
}

/// Get the event type
int get eventType => commandGetEventType(command);

/// Get the event type
@Deprecated('user event type instead')
int get codeCommand => eventType;

/// Base command
factory MidiEvent.base(int command) {
MidiEvent event;

Expand Down Expand Up @@ -174,19 +183,25 @@ abstract class MidiEvent {

/// Channel event.
abstract class ChannelEvent extends MidiEvent {
/// Channel
int get channel => MidiEvent.commandGetChannel(command);

/// Constructor
ChannelEvent();

ChannelEvent.withParam(int comand, int channel)
: super.withParam(MidiEvent.commandChannel(comand, channel));
/// Constructor with command and channel
ChannelEvent.withParam(int command, int channel)
: super.withParam(MidiEvent.commandChannel(command, channel));
}

/// Param1ByteEvent
abstract class Param1ByteEvent extends ChannelEvent {
int? _param1;

/// Constructor
Param1ByteEvent();

/// Constructor with command, channel and param1
Param1ByteEvent.withParam(super.command, super.channel, this._param1)
: super.withParam();

Expand Down

0 comments on commit c267d2c

Please sign in to comment.