Skip to content

Commit

Permalink
Refactoring IsDebug
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei committed Oct 15, 2024
1 parent aba1aff commit 0c6a63a
Show file tree
Hide file tree
Showing 31 changed files with 72 additions and 62 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ unused:
# go install honnef.co/go/tools/cmd/staticcheck@latest
staticcheck ./...

install: test
go install github.com/emicklei/melrose/cmd/melrose
install: test q

# quickly get me a new binary
q:
Expand Down
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- play(loop(sequence('A B C D G'))) cannot be stopped
- https://github.com/dedicatted/golang-cross-build-ghaction
- https://github.com/dedicatted/golang-cross-build-ghaction
- merge is not correct
2 changes: 1 addition & 1 deletion control/bpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s SetBPM) S() core.Sequence {
// performs the set operation
func (s SetBPM) Evaluate(ctx core.Context) error {
f := core.Float(s.bpm)
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("control.bpm set %.2f", f)
}
if f > 0.0 {
Expand Down
6 changes: 3 additions & 3 deletions control/knob.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ func (k *Knob) Storex() string {
}

func (k *Knob) NoteOn(channel int, n core.Note) {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("knob.NoteOn %v", n)
}
}
func (k *Knob) NoteOff(channel int, n core.Note) {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("knob.NoteOff %v", n)
}
}
func (k *Knob) ControlChange(channel, number, value int) {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("knob.ControlChange ch=%d,nr=%d,val=%d", channel, number, value)
}
// TODO check channel
Expand Down
4 changes: 2 additions & 2 deletions control/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (l *Listen) IsPlaying() bool {
// NoteOn is part of core.NoteListener
func (l *Listen) NoteOn(channel int, n core.Note) {
l.mutex.Lock()
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("control.listen ON %v", n)
}
l.noteChangeCount++
Expand Down Expand Up @@ -108,7 +108,7 @@ func (l *Listen) isNoteOnCount(nr, countCheck int) bool {
func (l *Listen) NoteOff(channel int, n core.Note) {
l.mutex.Lock()
defer l.mutex.Unlock()
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("control.listen OFF %v", n)
}
delete(l.notesOn, n.MIDI())
Expand Down
4 changes: 2 additions & 2 deletions control/onoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewOnOff(deviceID int, channel int, note core.Note) OnOff {

// Play implements Playable
func (o OnOff) Play(ctx core.Context, at time.Time) error {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("control.OnOff.Play dev=%d ch=%d note=%v", o.deviceID, o.channel, o.note)
}
return o.send(ctx, noteOn)
Expand All @@ -53,7 +53,7 @@ func (o OnOff) send(ctx core.Context, status int64) error {

// Stop implements Playable
func (o OnOff) Stop(ctx core.Context) error {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("control.OnOff.Stop dev=%d ch=%d note=%v", o.deviceID, o.channel, o.note)
}
return o.send(ctx, noteOff)
Expand Down
8 changes: 4 additions & 4 deletions control/recording.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ func (r *Recording) Play(ctx core.Context, at time.Time) error {
func (r *Recording) Stop(ctx core.Context) error {
// nothing there or already stopped
if r.timeline.Len() == 0 {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("empty timeline on stop recording")
}
return nil
}
seq := r.S()
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("recording.stop seq:%v", seq)
// TODO temporary store the recording for the test in ui/img/draw_test.go
fmt.Println("writing /tmp/melrose-recording.json")
Expand Down Expand Up @@ -76,7 +76,7 @@ func (r *Recording) S() core.Sequenceable {
func (r *Recording) NoteOn(channel int, n core.Note) {
when := time.Now()
change := core.NewNoteChange(true, int64(n.MIDI()), int64(n.Velocity))
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("recording.noteon note:%v t:%s", n, when.Format("04:05.000"))
}
r.timeline.Schedule(change, when)
Expand All @@ -85,7 +85,7 @@ func (r *Recording) NoteOn(channel int, n core.Note) {
func (r *Recording) NoteOff(channel int, n core.Note) {
when := time.Now()
change := core.NewNoteChange(false, int64(n.MIDI()), int64(n.Velocity))
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("recording.noteoff note:%v t:%s", n, when.Format("04:05.000"))
}
r.timeline.Schedule(change, when)
Expand Down
8 changes: 4 additions & 4 deletions core/beatmaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (b *Beatmaster) SettingNotifier(handler func(LoopController)) {
// bars is zero-based
func (b *Beatmaster) Plan(bars int64, seq Sequenceable) {
atBeats := b.beatsAtNextBar() + (b.biab * bars)
if IsDebug() {
if notify.IsDebug() {
notify.Debugf("beat.schedule at beats: %d put: %s bars: %.2f", atBeats, Storex(seq), seq.S().Bars(int(b.biab)))
}
b.schedule.Schedule(atBeats, func(when time.Time) {
Expand Down Expand Up @@ -132,7 +132,7 @@ func (b *Beatmaster) Start() {
b.ticker = time.NewTicker(beatTickerDuration(b.bpm))
b.beating = true
go func() {
if IsDebug() {
if notify.IsDebug() {
notify.Debugf("core.beatmaster: started bpm=%v tick=%v", b.bpm, beatTickerDuration(b.bpm))
}
for {
Expand All @@ -144,7 +144,7 @@ func (b *Beatmaster) Start() {
return
// only change BPM on a bar
case bpm := <-b.bpmChanges:
if IsDebug() {
if notify.IsDebug() {
notify.Debugf("core.beatmaster: changed bpm=%v tick=%v", bpm, beatTickerDuration(bpm))
}
b.bpm = bpm
Expand Down Expand Up @@ -185,7 +185,7 @@ func (b *Beatmaster) Stop() {
b.beating = false
b.ticker.Stop()
b.done <- true
if IsDebug() {
if notify.IsDebug() {
notify.Debugf("core.beatmaster: stopped")
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (l *Loop) Evaluate(ctx Context) error {
cond = with.Condition()
}
clone.condition = cond
if IsDebug() {
if notify.IsDebug() {
notify.Debugf("loop.eval")
}
clone.Play(l.ctx, time.Now())
Expand Down Expand Up @@ -89,7 +89,7 @@ func (l *Loop) reschedule(d AudioDevice, when time.Time) {
// after each other
moment = d.Play(l.condition, each, l.ctx.Control().BPM(), moment)
}
if IsDebug() {
if notify.IsDebug() {
notify.Debugf("core.loop: next=%s", moment.Format("15:04:05.00"))
}
// schedule the loop itself so it can play again when Handle is called
Expand Down
11 changes: 0 additions & 11 deletions core/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ import (
"github.com/emicklei/melrose/notify"
)

var debugEnabled = false

func IsDebug() bool {
return debugEnabled
}

func ToggleDebug() bool {
debugEnabled = !debugEnabled
return debugEnabled
}

type Print struct {
Context Context
Target interface{}
Expand Down
2 changes: 1 addition & 1 deletion core/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (t *Timeline) Play() {

// Reset forgets about all scheduled calls.
func (t *Timeline) Reset() {
if IsDebug() {
if notify.IsDebug() {
notify.Debugf("core.timeline: flushing all scheduled MIDI events")
}
t.protection.Lock()
Expand Down
2 changes: 1 addition & 1 deletion core/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (t *Track) Play(ctx Context, now time.Time) error {
cs := NewChannelSelector(each, On(t.Channel))
offset := int64((bars-1)*biab) * whole.Nanoseconds() / 4
when := now.Add(time.Duration(time.Duration(offset)))
if IsDebug() {
if notify.IsDebug() {
notify.Debugf("core.track title=%s channel=%d bar=%d, biab=%d, bpm=%.2f time=%s", t.Title, t.Channel, bars, biab, bpm, when.Format("04:05.000"))
}
ctx.Device().Play(NoCondition, cs, bpm, when)
Expand Down
4 changes: 2 additions & 2 deletions dsl/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ func (e *Evaluator) EvaluateExpression(entry string) (interface{}, error) {
// this can be requested from the editor to listen to a part of a sequence,chord,note,progression
if strings.Contains(entry, "/") {
if subchord, suberr := core.ParseChord(entry); suberr == nil {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("dsl.evaluate:%s", subchord.Storex())
}
return subchord, nil
}
}
// try parsing the entry as a sequence
if subseq, suberr := core.ParseSequence(entry); suberr == nil {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("dsl.evaluate:%s", subseq.Storex())
}
return subseq, nil
Expand Down
2 changes: 1 addition & 1 deletion midi/echo_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ type EchoListener struct {

func (e EchoListener) NoteOn(channel int, n core.Note) {}
func (e EchoListener) NoteOff(channel int, n core.Note) {
fmt.Fprintf(notify.Console.StandardOut, "%s xx", n.String())
fmt.Fprintf(notify.Console.StandardOut, "%s ", n.String())
}
func (e EchoListener) ControlChange(channel, number, value int) {}
6 changes: 3 additions & 3 deletions midi/key_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewKeyTrigger(ctx core.Context, channel int, onNote core.Note, startStop co

// NoteOn is part of core.NoteListener
func (t *KeyTrigger) NoteOn(channel int, n core.Note) {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("keytrigger.NoteOn ch=%d note=%v", channel, n)
}
if channel != t.channel {
Expand Down Expand Up @@ -73,14 +73,14 @@ func (t *KeyTrigger) NoteOn(channel int, n core.Note) {

// NoteOff is part of core.NoteListener
func (t *KeyTrigger) NoteOff(channel int, n core.Note) {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("keytrigger.NoteOff ch=%d note=%v", channel, n)
}
// key trigger is not interested in this
}

func (t *KeyTrigger) ControlChange(channel, number, value int) {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("keytrigger.ControlChange %d %d %d", channel, number, value)
}
// key trigger is not interested in this
Expand Down
2 changes: 1 addition & 1 deletion midi/midi_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (m Message) S() core.Sequence {
channel := core.Int(m.channel)
data1 := core.Int(m.data1)
data2 := core.Int(m.data2)
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("midi.message: device=%d, status=%d channel=%v data1=%v data2=%v", deviceID, m.status, channel, data1, data2)
}
devices := m.audioDevices.(*DeviceRegistry)
Expand Down
7 changes: 6 additions & 1 deletion midi/output_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ func (d *OutputDevice) Start() {
}

func (d *OutputDevice) Reset() {
defer func() {
if err := recover(); err != nil {
notify.Warnf("reset failed for device:%v", d.id)
}
}()
d.timeline.Reset()
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("device.%d: sending Note OFF to all 16 channels", d.id)
}
if d.stream != nil {
Expand Down
2 changes: 1 addition & 1 deletion midi/pedal_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (p pedalEvent) Handle(tim *core.Timeline, when time.Time) {
// MIDI CC 64 Damper Pedal /Sustain Pedal
status := controlChange | int64(p.channel-1)
_ = p.out.WriteShort(status, sustainPedal, onoff)
if core.IsDebug() {
if notify.IsDebug() {
msg := "down"
if !p.goingDown {
msg = "up"
Expand Down
2 changes: 1 addition & 1 deletion midi/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (r *DeviceRegistry) Schedule(e core.TimelineEvent, beginAt time.Time) {
// Play schedules all the notes on the timeline beginning at a give time (now or in the future).
// Returns the end time of the last played Note.
func (r *DeviceRegistry) Play(condition core.Condition, seq core.Sequenceable, bpm float64, beginAt time.Time) time.Time {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("midi.play: time=%s object=%s", beginAt.Format("04:05.000"), core.Storex(seq))
}
// unwrap if variable because we need to detect device or channel selector
Expand Down
2 changes: 1 addition & 1 deletion midi/registry_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r *DeviceRegistry) Command(args []string) notify.Message {
}
if args[0] == "e" {
if len(args) != 3 {
return notify.NewErrorf("missing`i` or `o` and device number")
return notify.NewErrorf("missing `i` or `o` and device number")
}
if args[1] != "i" && args[1] != "o" {
return notify.NewErrorf("first parameter is either `i` for input or `o` for output")
Expand Down
2 changes: 1 addition & 1 deletion midi/registry_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (r *DeviceRegistry) OnKey(ctx core.Context, deviceID int, channel int, note
}

func (r *DeviceRegistry) Listen(deviceID int, who core.NoteListener, startOrStop bool) {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("midi.listen id=%d, start=%v", deviceID, startOrStop)
}
in, err := r.Input(deviceID)
Expand Down
9 changes: 6 additions & 3 deletions midi/transport/m_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/emicklei/melrose/core"
"github.com/emicklei/melrose/notify"
)

type mNoteEvent struct {
Expand Down Expand Up @@ -92,11 +93,12 @@ func (l *mListener) HandleMIDIMessage(status int16, nr int, data2 int) {
if _, ok := l.noteOn[nr]; ok {
return
}
onNote, _ := core.MIDItoNote(0.25, nr, velocity)
onNote, _ := core.MIDItoNote(0.25, nr, velocity) // length is computed on note off
l.noteOn[nr] = mNoteEvent{
note: onNote,
when: time.Now(),
}
notify.Debugf("on %s", onNote)
for _, each := range l.noteListeners {
each.NoteOn(ch, onNote)
}
Expand All @@ -115,8 +117,9 @@ func (l *mListener) HandleMIDIMessage(status int16, nr int, data2 int) {
delete(l.noteOn, nr)
// compute delta
ms := time.Duration(time.Now().UnixNano()-on.when.UnixNano()) * time.Nanosecond
frac := core.DurationToFraction(120.0, ms) // TODO
offNote, _ := core.MIDItoNote(frac, nr, core.Normal)
frac := core.DurationToFraction(120.0, ms) // TODO, BPM
offNote, _ := core.MIDItoNote(frac, nr, on.note.Velocity)
notify.Debugf("off %s [%d]", offNote, ms)
for _, each := range l.noteListeners {
each.NoteOff(ch, offNote)
}
Expand Down
9 changes: 4 additions & 5 deletions midi/transport/rt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
package transport

import (
"github.com/emicklei/melrose/core"
"github.com/emicklei/melrose/notify"
"gitlab.com/gomidi/midi/v2/drivers/rtmididrv/imported/rtmidi"
)

func init() { Initializer = rtInitialize }

func rtInitialize() {
if core.IsDebug() {
if notify.IsDebug() {
notify.Debugf("transport.init: use RtmidiTransporter")
}
Factory = func() Transporter {
Expand Down Expand Up @@ -70,7 +69,7 @@ func (o RtmidiOut) WriteShort(status int64, data1 int64, data2 int64) error {
return o.out.SendMessage([]byte{byte(status & 0xFF), byte(data1 & 0xFF), byte(data2 & 0xFF)})
}
func (o RtmidiOut) Close() error {
if core.IsDebug() {
if notify.IsDebug() {
name, _ := o.out.PortName(o.port)
notify.Debugf("transport.RtmidiOut.Close: name=%s port=%d", name, o.port)
}
Expand All @@ -83,7 +82,7 @@ type RtmidiIn struct {
}

func (i RtmidiIn) Close() error {
if core.IsDebug() {
if notify.IsDebug() {
name, _ := i.in.PortName(i.port)
notify.Debugf("transport.RtmidiIn.Close: name=%s port=%d", name, i.port)
}
Expand Down Expand Up @@ -124,7 +123,7 @@ func (l *RtListener) handleRtEvent(m rtmidi.MIDIIn, data []byte, delta float64)
if !l.listening {
// consume event so the queue does not fill up

// if core.IsDebug() {
// if notify.IsDebug() {
// notify.Debugf("handle.rt.event:%v,%v\n", data, delta)
// }
return
Expand Down
Loading

0 comments on commit 0c6a63a

Please sign in to comment.