Unit test, fix and simplify units
This commit is contained in:
parent
2177c747aa
commit
a634415bff
1
go.mod
1
go.mod
|
@ -5,6 +5,7 @@ go 1.18
|
|||
require (
|
||||
git.gammaspectra.live/S.O.N.G/Kirika v0.0.0-20220713142734-894d8db69758
|
||||
git.gammaspectra.live/S.O.N.G/goborator v0.0.0-20220713201545-e92f79a950b7
|
||||
github.com/cocoonlife/testify v0.0.0-20160218172820-792cc1faeb64
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
1
go.sum
1
go.sum
|
@ -7,6 +7,7 @@ git.gammaspectra.live/S.O.N.G/goborator v0.0.0-20220713201545-e92f79a950b7/go.mo
|
|||
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220515172202-6e490998d2a0 h1:imcnwHUqaAJzws41B8sCSp/sUmVranNjAX205Jr4Jc0=
|
||||
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220515172202-6e490998d2a0/go.mod h1:/po1QgOh3xynbvi4sxdY6Iw8m5WPJfGGmry2boZD8fs=
|
||||
github.com/cocoonlife/testify v0.0.0-20160218172820-792cc1faeb64 h1:LjPYdzoFSAJ5Tr/ElL8kzTJghXgpnOjJVbgd1UvZB1o=
|
||||
github.com/cocoonlife/testify v0.0.0-20160218172820-792cc1faeb64/go.mod h1:LoCAz53rbPcqs8Da2BjB/yDy4gxMtiSQmqnYI/DGH+U=
|
||||
github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b/go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkEQxENCrlLo=
|
||||
github.com/dh1tw/gosamplerate v0.1.2 h1:oyqtZk67xB9B4l+vIZCZ3F0RYV/z66W58VOah11/ktI=
|
||||
github.com/dh1tw/gosamplerate v0.1.2/go.mod h1:zooTyHpoR7hE+FLfdE3yjLHb2QA2NpMusNfuaZqEACM=
|
||||
|
|
|
@ -121,12 +121,10 @@ func (i *Instance) BlockToDuration(t uint32, latency time.Duration) time.Duratio
|
|||
}
|
||||
|
||||
func (i *Instance) BinToHertz(bin uint32) Hertz {
|
||||
centsPerBin := 1200. / float64(i.GetTransformBandsPerOctave())
|
||||
centsPerBin := CentsPerOctave / float64(i.GetTransformBandsPerOctave())
|
||||
diffFromMinFreqInCents := Cent(float64(bin) * centsPerBin)
|
||||
minFreqInAbsCents := HertzToAbsoluteCent(i.TransformMinimumFrequency)
|
||||
binInAbsCents := minFreqInAbsCents + diffFromMinFreqInCents
|
||||
|
||||
return AbsoluteCentToHertz(binInAbsCents)
|
||||
return i.TransformMinimumFrequency.Cents(diffFromMinFreqInCents)
|
||||
}
|
||||
|
||||
func (i *Instance) GetStrategy(store Store, resamplerQuality ...filter.ResampleQuality) *Strategy {
|
||||
|
@ -141,11 +139,11 @@ func (i *Instance) GetTransformBandsPerOctave() int {
|
|||
return i.TransformBands / int(i.TransformMaximumFrequencyOffset)
|
||||
}
|
||||
func (i *Instance) GetTransformReferenceFrequencyInHertz() Hertz {
|
||||
return RelativeOctavesToHertz(i.TransformMinimumFrequency, i.TransformReferenceFrequencyOffset)
|
||||
return i.TransformMinimumFrequency.Octaves(i.TransformReferenceFrequencyOffset)
|
||||
}
|
||||
|
||||
func (i *Instance) GetTransformMaximumFrequencyInHertz() Hertz {
|
||||
return RelativeOctavesToHertz(i.TransformMinimumFrequency, i.TransformMaximumFrequencyOffset)
|
||||
return i.TransformMinimumFrequency.Octaves(i.TransformMaximumFrequencyOffset)
|
||||
}
|
||||
|
||||
func (i *Instance) GetTransformTimeResolutionInSamples() int {
|
||||
|
|
|
@ -3,20 +3,27 @@ package panako
|
|||
import "math"
|
||||
|
||||
type Hertz float64
|
||||
|
||||
// Cent Twelve-tone equal temperament divides the Octave into 12 semitones of 100 Cent each.
|
||||
type Cent int64
|
||||
|
||||
// Octave The interval between one musical pitch and another with double its frequency
|
||||
type Octave int
|
||||
|
||||
// CentsReferenceFrequency 16.35 / 2 Hz.
|
||||
const CentsReferenceFrequency = 8.17579892
|
||||
const CentsPerOctave = 1200
|
||||
|
||||
func AbsoluteCentToHertz(cents Cent) Hertz {
|
||||
return Hertz(CentsReferenceFrequency * math.Pow(2.0, float64(cents)/1200.))
|
||||
func (h Hertz) DifferenceInCent(b Hertz) Cent {
|
||||
return Cent(CentsPerOctave * math.Log2(float64(b/h)))
|
||||
}
|
||||
|
||||
func HertzToAbsoluteCent(hertzs Hertz) Cent {
|
||||
return Cent(1200. * math.Log(float64(hertzs/CentsReferenceFrequency)) / math.Log(2.))
|
||||
func (h Hertz) DifferenceInHertz(b Hertz) Hertz {
|
||||
return b - h
|
||||
}
|
||||
|
||||
func RelativeOctavesToHertz(hertz Hertz, octaves Octave) Hertz {
|
||||
return hertz * Hertz(int(1)<<octaves)
|
||||
func (h Hertz) Octaves(octaves Octave) Hertz {
|
||||
return h * Hertz(math.Pow(2, float64(octaves)))
|
||||
}
|
||||
|
||||
func (h Hertz) Cents(cents Cent) Hertz {
|
||||
return h * Hertz(math.Pow(2, float64(cents)/CentsPerOctave))
|
||||
}
|
||||
|
|
41
panako/units_test.go
Normal file
41
panako/units_test.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package panako
|
||||
|
||||
import (
|
||||
"github.com/cocoonlife/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOctavesUp(t *testing.T) {
|
||||
freq := Hertz(110)
|
||||
|
||||
assert.Equal(t, Hertz(220), freq.Octaves(1), "110Hz -> 220Hz")
|
||||
assert.Equal(t, Hertz(440), freq.Octaves(2), "110Hz -> 440Hz")
|
||||
assert.Equal(t, Hertz(7040), freq.Octaves(6), "110Hz -> 7040Hz")
|
||||
}
|
||||
|
||||
func TestOctavesDown(t *testing.T) {
|
||||
freq := Hertz(440)
|
||||
|
||||
assert.Equal(t, Hertz(220), freq.Octaves(-1), "440Hz -> 220Hz")
|
||||
assert.Equal(t, Hertz(110), freq.Octaves(-2), "440Hz -> 110Hz")
|
||||
}
|
||||
|
||||
func TestHertz(t *testing.T) {
|
||||
freq := Hertz(110)
|
||||
|
||||
assert.Equal(t, CentsPerOctave, freq.DifferenceInCent(Hertz(220)), "Cents 110Hz -> 220Hz")
|
||||
assert.Equal(t, CentsPerOctave*2, freq.DifferenceInCent(Hertz(440)), "Cents 110Hz -> 440Hz")
|
||||
assert.Equal(t, CentsPerOctave*6, freq.DifferenceInCent(Hertz(7040)), "Cents 110Hz -> 7040Hz")
|
||||
|
||||
assert.Equal(t, Hertz(110), freq.DifferenceInHertz(Hertz(220)), "Hertz 110Hz -> 220Hz")
|
||||
assert.Equal(t, Hertz(330), freq.DifferenceInHertz(Hertz(440)), "Hertz 110Hz -> 440Hz")
|
||||
assert.Equal(t, Hertz(6930), freq.DifferenceInHertz(Hertz(7040)), "Hertz 110Hz -> 7040Hz")
|
||||
}
|
||||
|
||||
func TestCents(t *testing.T) {
|
||||
freq := Hertz(110)
|
||||
|
||||
assert.Equal(t, Hertz(220), freq.Cents(CentsPerOctave), "110Hz -> 220Hz")
|
||||
assert.Equal(t, Hertz(440), freq.Cents(CentsPerOctave*2), "110Hz -> 440Hz")
|
||||
assert.Equal(t, Hertz(7040), freq.Cents(CentsPerOctave*6), "110Hz -> 7040Hz")
|
||||
}
|
Loading…
Reference in a new issue