Skip to content

Commit

Permalink
Working somewhat, phase issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aprice2704 committed Jan 23, 2021
1 parent 0ff4836 commit e8e8fbc
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 118 deletions.
Binary file added Go-Mono-Bold-Italic.ttf
Binary file not shown.
Binary file added Go-Mono-Bold.ttf
Binary file not shown.
Binary file added Go-Mono-Italic.ttf
Binary file not shown.
Binary file added Go-Mono.ttf
Binary file not shown.
107 changes: 58 additions & 49 deletions envelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package main
// They should be normalized to have values between 0 and 1

import (
"fmt"
"math"
)

Expand All @@ -21,10 +20,9 @@ var (

// Enveloper modulates a signal
type Enveloper interface {
Amplitude(t Seconds) float64 // Call this from outside
OnePeriodAmplitude(t Seconds) float64 // Implement this
SetPeriodandLength(λ Seconds, length Seconds) // Set both the period (λ) field and length
Length() Seconds // Return overall length of the envelope
Amplitude(t Seconds) float64 // Call this from outside
OnePeriodAmplitude(t Seconds) float64 // Implement this
Length() Seconds // Return overall length of the envelope
}

// Envelope is the 'base' type for envelopes
Expand All @@ -34,30 +32,9 @@ type Envelope struct {
Len Seconds // The overall length of the envelope (might be several λ long)
}

// SetPeriodandLength fulfils Enveloper interface
func (e *Envelope) SetPeriodandLength(λ Seconds, length Seconds) {
e.λ = λ
e.Len = length
}

// OnePeriodAmplitude is the function you should implement for new types of envelope
func (e *Envelope) OnePeriodAmplitude(t Seconds) float64 {
fmt.Printf("Warning: Amplitude called on base Envelope class, probably an error\n")
return 1
}

// Amplitude is the function to call from outide the class to get either the single shot envelope or the repeated one
// If you implemented OneShotAmplitude, this should give you repeats for free
func (e *Envelope) Amplitude(t Seconds) float64 {
if e.Repeats {
return e.OnePeriodAmplitude(Seconds(math.Mod(float64(t), float64(e.λ))))
}
return e.OnePeriodAmplitude(t)
}

// Length is
func (e *Envelope) Length() Seconds {
return e.Len
// NewEnvelope is self-evident
func NewEnvelope(λ Seconds, reps bool, l Seconds) *Envelope {
return &Envelope{λ: λ, Repeats: reps, Len: l}
}

// Gaussian is an envelope with height 1 at μ and RMS width of σ
Expand All @@ -68,9 +45,10 @@ type Gaussian struct {
σσ Seconds
}

// NewGaussian make a new one
func NewGaussian(newμ, newσ Seconds) *Gaussian {
g := &Gaussian{μ: newμ, σ: newσ, σσ: newσ * newσ}
// NewGaussian makes a new one
func NewGaussian(λ Seconds, reps bool, l Seconds, newμ, newσ Seconds) *Gaussian {
e := NewEnvelope(λ, reps, l)
g := &Gaussian{Envelope: *e, μ: newμ, σ: newσ, σσ: newσ * newσ}
return g
}

Expand All @@ -81,23 +59,20 @@ func (g *Gaussian) OnePeriodAmplitude(x Seconds) float64 {
return math.Exp(-xu * xu / float64(2*g.σσ))
}

// RepeatAmplitude generates a sequence of gaussian envelopes of period λ seconds
// func (g *Gaussian) OneShotAmplitude(t Seconds) float64 {

// s := math.Mod(float64(t), float64(g.λ))
// tu := s - float64(g.μ)
// return math.Exp(-tu * tu / float64(2*g.σσ))

// }

// Triangle a simple /\ with period λ
type Triangle struct {
Envelope
*Envelope
}

// NewTriangle is self-evident
func NewTriangle(λ Seconds, reps bool, l Seconds) *Triangle {
e := NewEnvelope(λ, reps, l)
return &Triangle{Envelope: e}
}

// Amplitude is
func (tr Triangle) Amplitude(t Seconds) float64 {
return tr.Envelope.Amplitude(t)
return tr.OnePeriodAmplitude(Seconds(math.Mod(float64(t), float64(tr.λ))))
}

// OnePeriodAmplitude is
Expand All @@ -109,14 +84,9 @@ func (tr Triangle) OnePeriodAmplitude(t Seconds) float64 {
return float64(2 - (t * 2 / tr.λ))
}

// SetPeriodandLength fulfils Enveloper interface
func (tr Triangle) SetPeriodandLength(λ Seconds, length Seconds) {
tr.Envelope.SetPeriodandLength(λ, length)
}

// Length is
func (tr Triangle) Length() Seconds {
return tr.Envelope.Length()
return tr.Len
}

// RepeatAmplitude is
Expand All @@ -127,3 +97,42 @@ func (tr Triangle) Length() Seconds {
// }
// return 2 - (s * 2 / float64(tr.λ))
// }
// SetPeriodandLength fulfils Enveloper interface
// func (tr Triangle) SetPeriodandLength(λ Seconds, length Seconds) {
// tr.Envelope.SetPeriodandLength(λ, length)
// }

// RepeatAmplitude generates a sequence of gaussian envelopes of period λ seconds
// func (g *Gaussian) OneShotAmplitude(t Seconds) float64 {

// s := math.Mod(float64(t), float64(g.λ))
// tu := s - float64(g.μ)
// return math.Exp(-tu * tu / float64(2*g.σσ))

// }
// OnePeriodAmplitude is the function you should implement for new types of envelope
// func (e *Envelope) OnePeriodAmplitude(t Seconds) float64 {
// fmt.Printf("Warning: Amplitude called on base Envelope class, probably an error\n")
// return 1
// }

// Amplitude is the function to call from outide the class to get either the single shot envelope or the repeated one
// If you implemented OneShotAmplitude, this should give you repeats for free
// func (e *Envelope) Amplitude(t Seconds) float64 {
// if e.Repeats {
// return e.OnePeriodAmplitude(Seconds(math.Mod(float64(t), float64(e.λ))))
// }
// return e.OnePeriodAmplitude(t)
// }

// Length is
// func (e *Envelope) Length() Seconds {
// return e.Len
// }

// SetPeriodandLength fulfils Enveloper interface
// func (e *Envelope) SetPeriodandLength(λ Seconds, length Seconds) {
// e.λ = λ
// e.Len = length
// }
// SetPeriodandLength(λ Seconds, length Seconds) // Set both the period (λ) field and length
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/aprice2704/jmj
go 1.15

require (
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807
github.com/faiface/beep v1.0.2
github.com/veandco/go-sdl2 v0.4.5
golang.org/x/image v0.0.0-20201208152932-35266b937fa6 // indirect
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 // indirect
)
Binary file added line.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e8e8fbc

Please sign in to comment.