mirror of
https://github.com/vsariola/sointu.git
synced 2025-05-25 18:00:37 -04:00
refactor(tracker): rename trySend to TrySend to make it public
This commit is contained in:
parent
0199658025
commit
9f89c37956
@ -211,7 +211,7 @@ func (m *Model) Undo() Action {
|
||||
m.d = m.undoStack[len(m.undoStack)-1]
|
||||
m.undoStack = m.undoStack[:len(m.undoStack)-1]
|
||||
m.prevUndoKind = ""
|
||||
trySend(m.broker.ToPlayer, any(m.d.Song.Copy()))
|
||||
TrySend(m.broker.ToPlayer, any(m.d.Song.Copy()))
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -228,7 +228,7 @@ func (m *Model) Redo() Action {
|
||||
m.d = m.redoStack[len(m.redoStack)-1]
|
||||
m.redoStack = m.redoStack[:len(m.redoStack)-1]
|
||||
m.prevUndoKind = ""
|
||||
trySend(m.broker.ToPlayer, any(m.d.Song.Copy()))
|
||||
TrySend(m.broker.ToPlayer, any(m.d.Song.Copy()))
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -318,7 +318,7 @@ func (m *Model) PlayCurrentPos() Action {
|
||||
m.setPanic(false)
|
||||
m.setLoop(Loop{})
|
||||
m.playing = true
|
||||
trySend(m.broker.ToPlayer, any(StartPlayMsg{m.d.Cursor.SongPos}))
|
||||
TrySend(m.broker.ToPlayer, any(StartPlayMsg{m.d.Cursor.SongPos}))
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -330,7 +330,7 @@ func (m *Model) PlaySongStart() Action {
|
||||
m.setPanic(false)
|
||||
m.setLoop(Loop{})
|
||||
m.playing = true
|
||||
trySend(m.broker.ToPlayer, any(StartPlayMsg{}))
|
||||
TrySend(m.broker.ToPlayer, any(StartPlayMsg{}))
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -345,7 +345,7 @@ func (m *Model) PlaySelected() Action {
|
||||
r := l.listRange()
|
||||
newLoop := Loop{r.Start, r.End - r.Start}
|
||||
m.setLoop(newLoop)
|
||||
trySend(m.broker.ToPlayer, any(StartPlayMsg{sointu.SongPos{OrderRow: r.Start, PatternRow: 0}}))
|
||||
TrySend(m.broker.ToPlayer, any(StartPlayMsg{sointu.SongPos{OrderRow: r.Start, PatternRow: 0}}))
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -360,7 +360,7 @@ func (m *Model) PlayFromLoopStart() Action {
|
||||
return
|
||||
}
|
||||
m.playing = true
|
||||
trySend(m.broker.ToPlayer, any(StartPlayMsg{sointu.SongPos{OrderRow: m.loop.Start, PatternRow: 0}}))
|
||||
TrySend(m.broker.ToPlayer, any(StartPlayMsg{sointu.SongPos{OrderRow: m.loop.Start, PatternRow: 0}}))
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -375,7 +375,7 @@ func (m *Model) StopPlaying() Action {
|
||||
return
|
||||
}
|
||||
m.playing = false
|
||||
trySend(m.broker.ToPlayer, any(IsPlayingMsg{false}))
|
||||
TrySend(m.broker.ToPlayer, any(IsPlayingMsg{false}))
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -517,13 +517,13 @@ func (m *Model) completeAction(checkSave bool) {
|
||||
func (m *Model) setPanic(val bool) {
|
||||
if m.panic != val {
|
||||
m.panic = val
|
||||
trySend(m.broker.ToPlayer, any(PanicMsg{val}))
|
||||
TrySend(m.broker.ToPlayer, any(PanicMsg{val}))
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Model) setLoop(newLoop Loop) {
|
||||
if m.loop != newLoop {
|
||||
m.loop = newLoop
|
||||
trySend(m.broker.ToPlayer, any(newLoop))
|
||||
TrySend(m.broker.ToPlayer, any(newLoop))
|
||||
}
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ func (m *IsRecording) Value() bool { return (*Model)(m).recording }
|
||||
func (m *IsRecording) setValue(val bool) {
|
||||
m.recording = val
|
||||
m.instrEnlarged = val
|
||||
trySend(m.broker.ToPlayer, any(RecordingMsg{val}))
|
||||
TrySend(m.broker.ToPlayer, any(RecordingMsg{val}))
|
||||
}
|
||||
func (m *IsRecording) Enabled() bool { return true }
|
||||
|
||||
@ -86,9 +86,9 @@ func (m *Playing) setValue(val bool) {
|
||||
m.playing = val
|
||||
if m.playing {
|
||||
(*Model)(m).setPanic(false)
|
||||
trySend(m.broker.ToPlayer, any(StartPlayMsg{m.d.Cursor.SongPos}))
|
||||
TrySend(m.broker.ToPlayer, any(StartPlayMsg{m.d.Cursor.SongPos}))
|
||||
} else {
|
||||
trySend(m.broker.ToPlayer, any(IsPlayingMsg{val}))
|
||||
TrySend(m.broker.ToPlayer, any(IsPlayingMsg{val}))
|
||||
}
|
||||
}
|
||||
func (m *Playing) Enabled() bool { return m.playing || !m.instrEnlarged }
|
||||
@ -144,7 +144,7 @@ func (m *Oversampling) Bool() Bool { return Bool{m} }
|
||||
func (m *Oversampling) Value() bool { return m.oversampling }
|
||||
func (m *Oversampling) setValue(val bool) {
|
||||
m.oversampling = val
|
||||
trySend(m.broker.ToDetector, MsgToDetector{HasOversampling: true, Oversampling: val})
|
||||
TrySend(m.broker.ToDetector, MsgToDetector{HasOversampling: true, Oversampling: val})
|
||||
}
|
||||
func (m *Oversampling) Enabled() bool { return true }
|
||||
|
||||
|
@ -85,10 +85,10 @@ func (b *Broker) PutAudioBuffer(buf *sointu.AudioBuffer) {
|
||||
b.bufferPool.Put(buf)
|
||||
}
|
||||
|
||||
// trySend is a helper function to send a value to a channel if it is not full.
|
||||
// TrySend is a helper function to send a value to a channel if it is not full.
|
||||
// It is guaranteed to be non-blocking. Return true if the value was sent, false
|
||||
// otherwise.
|
||||
func trySend[T any](c chan<- T, v T) bool {
|
||||
func TrySend[T any](c chan<- T, v T) bool {
|
||||
select {
|
||||
case c <- v:
|
||||
default:
|
||||
|
@ -139,7 +139,7 @@ func (s *Detector) Run() {
|
||||
break
|
||||
}
|
||||
}
|
||||
trySend(s.broker.ToModel, MsgToModel{
|
||||
TrySend(s.broker.ToModel, MsgToModel{
|
||||
HasDetectorResult: true,
|
||||
DetectorResult: DetectorResult{
|
||||
Loudness: s.loudnessDetector.update(chunk),
|
||||
|
@ -84,17 +84,17 @@ func (m *Model) WriteWav(w io.WriteCloser, pcm16 bool) {
|
||||
name := fmt.Sprintf("%x", b)[2 : 32+2]
|
||||
data, err := sointu.Play(m.synther, song, func(p float32) {
|
||||
txt := fmt.Sprintf("Exporting song: %.0f%%", p*100)
|
||||
trySend(m.broker.ToModel, MsgToModel{Data: Alert{Message: txt, Priority: Info, Name: name, Duration: defaultAlertDuration}})
|
||||
TrySend(m.broker.ToModel, MsgToModel{Data: Alert{Message: txt, Priority: Info, Name: name, Duration: defaultAlertDuration}})
|
||||
}) // render the song to calculate its length
|
||||
if err != nil {
|
||||
txt := fmt.Sprintf("Error rendering the song during export: %v", err)
|
||||
trySend(m.broker.ToModel, MsgToModel{Data: Alert{Message: txt, Priority: Error, Name: name, Duration: defaultAlertDuration}})
|
||||
TrySend(m.broker.ToModel, MsgToModel{Data: Alert{Message: txt, Priority: Error, Name: name, Duration: defaultAlertDuration}})
|
||||
return
|
||||
}
|
||||
buffer, err := data.Wav(pcm16)
|
||||
if err != nil {
|
||||
txt := fmt.Sprintf("Error converting to .wav: %v", err)
|
||||
trySend(m.broker.ToModel, MsgToModel{Data: Alert{Message: txt, Priority: Error, Name: name, Duration: defaultAlertDuration}})
|
||||
TrySend(m.broker.ToModel, MsgToModel{Data: Alert{Message: txt, Priority: Error, Name: name, Duration: defaultAlertDuration}})
|
||||
return
|
||||
}
|
||||
w.Write(buffer)
|
||||
|
@ -135,7 +135,7 @@ func (v *DetectorWeighting) Int() Int { return Int{v} }
|
||||
func (v *DetectorWeighting) Value() int { return int(v.weightingType) }
|
||||
func (v *DetectorWeighting) setValue(value int) {
|
||||
v.weightingType = WeightingType(value)
|
||||
trySend(v.broker.ToDetector, MsgToDetector{HasWeightingType: true, WeightingType: WeightingType(value)})
|
||||
TrySend(v.broker.ToDetector, MsgToDetector{HasWeightingType: true, WeightingType: WeightingType(value)})
|
||||
}
|
||||
func (v *DetectorWeighting) Range() intRange { return intRange{0, int(NumLoudnessTypes) - 1} }
|
||||
func (v *DetectorWeighting) change(kind string) func() { return func() {} }
|
||||
|
@ -201,7 +201,7 @@ func NewModel(broker *Broker, synther sointu.Synther, midiContext MIDIContext, r
|
||||
}
|
||||
}
|
||||
}
|
||||
trySend(broker.ToPlayer, any(m.d.Song.Copy())) // we should be non-blocking in the constructor
|
||||
TrySend(broker.ToPlayer, any(m.d.Song.Copy())) // we should be non-blocking in the constructor
|
||||
m.signalAnalyzer = NewScopeModel(broker, m.d.Song.BPM)
|
||||
m.initDerivedData()
|
||||
return m
|
||||
@ -238,7 +238,7 @@ func (m *Model) change(kind string, t ChangeType, severity ChangeSeverity) func(
|
||||
m.d.Cursor.SongPos = m.d.Song.Score.Clamp(m.d.Cursor.SongPos)
|
||||
m.d.Cursor2.SongPos = m.d.Song.Score.Clamp(m.d.Cursor2.SongPos)
|
||||
m.updateDerivedScoreData()
|
||||
trySend(m.broker.ToPlayer, any(m.d.Song.Score.Copy()))
|
||||
TrySend(m.broker.ToPlayer, any(m.d.Song.Score.Copy()))
|
||||
}
|
||||
if m.changeType&PatchChange != 0 {
|
||||
m.fixIDCollisions()
|
||||
@ -254,14 +254,14 @@ func (m *Model) change(kind string, t ChangeType, severity ChangeSeverity) func(
|
||||
m.d.UnitSearching = false // if we change anything in the patch, reset the unit searching
|
||||
m.d.UnitSearchString = ""
|
||||
m.updateDerivedPatchData()
|
||||
trySend(m.broker.ToPlayer, any(m.d.Song.Patch.Copy()))
|
||||
TrySend(m.broker.ToPlayer, any(m.d.Song.Patch.Copy()))
|
||||
}
|
||||
if m.changeType&BPMChange != 0 {
|
||||
trySend(m.broker.ToPlayer, any(BPMMsg{m.d.Song.BPM}))
|
||||
TrySend(m.broker.ToPlayer, any(BPMMsg{m.d.Song.BPM}))
|
||||
m.signalAnalyzer.SetBpm(m.d.Song.BPM)
|
||||
}
|
||||
if m.changeType&RowsPerBeatChange != 0 {
|
||||
trySend(m.broker.ToPlayer, any(RowsPerBeatMsg{m.d.Song.RowsPerBeat}))
|
||||
TrySend(m.broker.ToPlayer, any(RowsPerBeatMsg{m.d.Song.RowsPerBeat}))
|
||||
}
|
||||
m.undoSkipCounter++
|
||||
var limit int
|
||||
@ -342,7 +342,7 @@ func (m *Model) UnmarshalRecovery(bytes []byte) {
|
||||
}
|
||||
}
|
||||
m.d.ChangedSinceRecovery = false
|
||||
trySend(m.broker.ToPlayer, any(m.d.Song.Copy()))
|
||||
TrySend(m.broker.ToPlayer, any(m.d.Song.Copy()))
|
||||
m.initDerivedData()
|
||||
}
|
||||
|
||||
@ -395,18 +395,18 @@ func (m *Model) Broker() *Broker { return m.broker }
|
||||
|
||||
func (m *Model) TrackNoteOn(track int, note byte) (id NoteID) {
|
||||
id = NoteID{IsInstr: false, Track: track, Note: note, model: m}
|
||||
trySend(m.broker.ToPlayer, any(NoteOnMsg{id}))
|
||||
TrySend(m.broker.ToPlayer, any(NoteOnMsg{id}))
|
||||
return id
|
||||
}
|
||||
|
||||
func (m *Model) InstrNoteOn(instr int, note byte) (id NoteID) {
|
||||
id = NoteID{IsInstr: true, Instr: instr, Note: note, model: m}
|
||||
trySend(m.broker.ToPlayer, any(NoteOnMsg{id}))
|
||||
TrySend(m.broker.ToPlayer, any(NoteOnMsg{id}))
|
||||
return id
|
||||
}
|
||||
|
||||
func (n NoteID) NoteOff() {
|
||||
trySend(n.model.broker.ToPlayer, any(NoteOffMsg{n}))
|
||||
TrySend(n.model.broker.ToPlayer, any(NoteOffMsg{n}))
|
||||
}
|
||||
|
||||
func (d *modelData) Copy() modelData {
|
||||
|
@ -153,7 +153,7 @@ func (p *Player) Process(buffer sointu.AudioBuffer, context PlayerProcessContext
|
||||
|
||||
bufPtr := p.broker.GetAudioBuffer() // borrow a buffer from the broker
|
||||
*bufPtr = append(*bufPtr, buffer[:rendered]...)
|
||||
if len(*bufPtr) == 0 || !trySend(p.broker.ToModel, MsgToModel{Data: bufPtr}) {
|
||||
if len(*bufPtr) == 0 || !TrySend(p.broker.ToModel, MsgToModel{Data: bufPtr}) {
|
||||
// if the buffer is empty or sending the rendered waveform to Model
|
||||
// failed, return the buffer to the broker
|
||||
p.broker.PutAudioBuffer(bufPtr)
|
||||
@ -249,7 +249,7 @@ loop:
|
||||
p.releaseTrack(i)
|
||||
}
|
||||
} else {
|
||||
trySend(p.broker.ToModel, MsgToModel{Reset: true})
|
||||
TrySend(p.broker.ToModel, MsgToModel{Reset: true})
|
||||
}
|
||||
case BPMMsg:
|
||||
p.song.BPM = m.int
|
||||
@ -268,7 +268,7 @@ loop:
|
||||
p.releaseTrack(i)
|
||||
}
|
||||
}
|
||||
trySend(p.broker.ToModel, MsgToModel{Reset: true})
|
||||
TrySend(p.broker.ToModel, MsgToModel{Reset: true})
|
||||
case NoteOnMsg:
|
||||
if m.IsInstr {
|
||||
p.triggerInstrument(m.Instr, m.Note)
|
||||
@ -346,7 +346,7 @@ func (p *Player) compileOrUpdateSynth() {
|
||||
|
||||
// all sendTargets from player are always non-blocking, to ensure that the player thread cannot end up in a dead-lock
|
||||
func (p *Player) send(message interface{}) {
|
||||
trySend(p.broker.ToModel, MsgToModel{HasPanicPosLevels: true, Panic: p.synth == nil, SongPosition: p.songPos, VoiceLevels: p.voiceLevels, Data: message})
|
||||
TrySend(p.broker.ToModel, MsgToModel{HasPanicPosLevels: true, Panic: p.synth == nil, SongPosition: p.songPos, VoiceLevels: p.voiceLevels, Data: message})
|
||||
}
|
||||
|
||||
func (p *Player) triggerInstrument(instrument int, note byte) {
|
||||
@ -402,7 +402,7 @@ func (p *Player) trigger(voiceStart, voiceEnd int, note byte, ID int) {
|
||||
p.voices[oldestVoice] = voice{noteID: ID, sustain: true, samplesSinceEvent: 0}
|
||||
p.voiceLevels[oldestVoice] = 1.0
|
||||
p.synth.Trigger(oldestVoice, note)
|
||||
trySend(p.broker.ToModel, MsgToModel{TriggerChannel: instrIndex + 1})
|
||||
TrySend(p.broker.ToModel, MsgToModel{TriggerChannel: instrIndex + 1})
|
||||
}
|
||||
|
||||
func (p *Player) release(ID int) {
|
||||
|
@ -106,7 +106,7 @@ func (s *ScopeModel) ProcessAudioBuffer(bufPtr *sointu.AudioBuffer) {
|
||||
s.waveForm.WriteOnce(*bufPtr)
|
||||
}
|
||||
// chain the messages: when we have a new audio buffer, try passing it on to the detector
|
||||
if !trySend(s.broker.ToDetector, MsgToDetector{Data: bufPtr}) {
|
||||
if !TrySend(s.broker.ToDetector, MsgToDetector{Data: bufPtr}) {
|
||||
s.broker.PutAudioBuffer(bufPtr)
|
||||
}
|
||||
}
|
||||
@ -125,7 +125,7 @@ func (s *ScopeModel) Reset() {
|
||||
l := len(s.waveForm.Buffer)
|
||||
s.waveForm.Buffer = s.waveForm.Buffer[:0]
|
||||
s.waveForm.Buffer = append(s.waveForm.Buffer, make([][2]float32, l)...)
|
||||
trySend(s.broker.ToDetector, MsgToDetector{Reset: true}) // chain the messages: when the signal analyzer is reset, also reset the detector
|
||||
TrySend(s.broker.ToDetector, MsgToDetector{Reset: true}) // chain the messages: when the signal analyzer is reset, also reset the detector
|
||||
}
|
||||
|
||||
func (s *ScopeModel) SetBpm(bpm int) {
|
||||
|
Loading…
Reference in New Issue
Block a user