Skip to content
Permalink
Browse files Browse the repository at this point in the history
Another bug fix to prevent two improvisations
  • Loading branch information
schollz committed Sep 4, 2017
1 parent 8df1334 commit f2964f2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
8 changes: 4 additions & 4 deletions ai2/ai.go
Expand Up @@ -67,8 +67,8 @@ func New(ticksPerBeat int) (ai *AI) {
ai.hasher.Salt = "piano"
ai.hasher.MinLength = 8
ai.LinkLength = 3
ai.WindowSizeMin = 30
ai.WindowSizeMax = 50
ai.WindowSizeMin = 20
ai.WindowSizeMax = 40
ai.Jazzy = true
ai.DisallowChords = true
ai.MaxChordDistance = 6 // DEPRECATED?
Expand Down Expand Up @@ -97,7 +97,7 @@ func (ai *AI) Learn(mus *music.Music) (err error) {
logger := log.WithFields(log.Fields{
"function": "AI.Analyze",
})
if len(mus.Notes) < 30 {
if len(mus.Notes) < ai.WindowSizeMax {
return errors.New("Too few notes")
}
logger.Debug("Analyzing...")
Expand Down Expand Up @@ -179,7 +179,7 @@ func (ai *AI) Learn(mus *music.Music) (err error) {
ai.chordArray = ai.chordArray[:chordArrayI]
ai.chordStringArray = ai.chordStringArray[:chordArrayI]
logger.Debugf("...analyzed %d chords", len(ai.chordArray))
if len(ai.chordArray) < 50 {
if len(ai.chordArray) < ai.WindowSizeMax {
return errors.New("Need more notes")
}
ai.HasLearned = true
Expand Down
6 changes: 5 additions & 1 deletion main.go
Expand Up @@ -53,6 +53,10 @@ func main() {
Name: "debug",
Usage: "debug mode",
},
cli.BoolFlag{
Name: "manual",
Usage: "AI is activated manually",
},
cli.IntFlag{
Name: "link",
Value: 3,
Expand Down Expand Up @@ -105,7 +109,7 @@ func main() {
p.AI.Jazzy = c.GlobalBool("jazzy")
p.AI.Stacatto = c.GlobalBool("stacatto")
p.AI.DisallowChords = !c.GlobalBool("chords")

p.ManualAI = c.GlobalBool("manual")
p.Start()
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions music/music.go
Expand Up @@ -104,6 +104,18 @@ func (m *Music) Get(beat int) (hasNotes bool, notes []Note) {
return
}

// HasFuture returns whether there are future beats in the registry
func (m *Music) HasFuture(currentBeat int) bool {
m.RLock()
defer m.RUnlock()
for beat := range m.Notes {
if beat > currentBeat {
return true
}
}
return false
}

// GetAll retrieve notes in music in a thread-safe way
func (m *Music) GetAll() (notes []Note) {
logger := log.WithFields(log.Fields{
Expand Down
27 changes: 21 additions & 6 deletions player/player.go
Expand Up @@ -63,6 +63,11 @@ type Player struct {
TicksPerBeat int
// 1/Quantize = shortest possible note
Quantize int

// flag to allow only manually activation
ManualAI bool

LastHostPress int
}

// New initializes the parameters and connects up the piano
Expand Down Expand Up @@ -161,11 +166,13 @@ func (p *Player) Start() {
p.Tick += 1
go p.Emit(p.Tick)

// if p.Tick-p.lastNote > (p.TicksPerBeat*p.BeatsOfSilence) && p.KeysCurrentlyPressed == 0 {
// logger.Info("Silence exceeded, trying to improvise")
// p.lastNote = p.Tick
// go p.Improvisation()
// }
if !p.ManualAI {
if p.Tick-p.lastNote > (p.TicksPerBeat*p.BeatsOfSilence) && p.KeysCurrentlyPressed == 0 {
logger.Info("Silence exceeded, trying to improvise")
p.lastNote = p.Tick
go p.Improvisation()
}
}

// if math.Mod(float64(p.Tick), 64) == 0 {
// logger.WithFields(log.Fields{
Expand Down Expand Up @@ -201,12 +208,17 @@ func (p *Player) Improvisation() {
logger := log.WithFields(log.Fields{
"function": "Player.Improvisation",
})
if p.MusicFuture.HasFuture(p.Tick) {
logger.Debug("Improvising is already in progress")
return
}
if !p.AI.HasLearned {
err := p.Teach()
if err != nil {
return
}
}
logger.Info("Getting improvisation")
notes, err := p.AI.Lick(p.Tick)
if err != nil {
logger.Error(err.Error())
Expand All @@ -223,7 +235,9 @@ func (p *Player) Improvisation() {
func (p *Player) Emit(beat int) {
hasNotes, notes := p.MusicFuture.Get(beat)
if hasNotes {
go p.Piano.PlayNotes(notes, p.BPM)
if p.Tick-p.LastHostPress > p.BeatsOfSilence*p.TicksPerBeat && p.KeysCurrentlyPressed == 0 {
go p.Piano.PlayNotes(notes, p.BPM)
}
p.lastNote = p.Tick
}
}
Expand Down Expand Up @@ -285,6 +299,7 @@ func (p *Player) Listen() {
p.KeysCurrentlyPressed--
}
if note.On && note.Pitch > p.HighPassFilter {
p.LastHostPress = p.Tick
p.KeysCurrentlyPressed++
}
logger.Infof("Adding %+v", note)
Expand Down

0 comments on commit f2964f2

Please sign in to comment.