add utilautest
1 files changed, 119 insertions(+), 0 deletions(-)

A => player/utilautest.go
A => player/utilautest.go +119 -0
@@ 0,0 1,119 @@ 
+package player
+
+import (
+	"fmt"
+	//"os"
+	"math"
+	"math/rand"
+)
+
+// move to player/utilautest ?
+
+// Generate sine sample data
+// It is float
+// It is stereo buffer / sine wave
+// freq = frequency in hz
+// bufSize = wave/buffer length in samples (bufSize) ?
+// XXX not continuous ?
+// not tested
+func GenSineBuf(sr int, freq int, bufSize int) [][2]float64 {
+	fmt.Println("GenSineBuf sr:", sr, "freq:", freq, "bufSize:", bufSize)
+//func GenSineBuf(freq int, bufSize int) [][2]float64 {
+//	fmt.Println("GenSineBuf freq:", freq, "bufSize:", bufSize)
+
+	amp := 1.0
+	_ = amp
+
+	// Info
+	// https://dev.to/thilanka/sine-wave-generator-using-golang-nom
+
+	/*
+	// dPhase ?
+	dt := freq / float64(sr)
+
+	if dt >= 1.0/2.0 {
+		panic("samplerate must be at least 2 times grater then frequency")
+	}
+	*/
+
+	// XXX test
+	//phaseInc := freq / float64(sr)
+	// f/fs ?
+	phaseInc := float64(freq) / float64(sr)
+	phase := 0.0
+	_ = phase
+
+	fmt.Println("phaseInc:", phaseInc)
+	//panic(2)
+
+	// Out data ?
+	outBuf := make([][2]float64, bufSize)
+
+	//numSamples := int(fs * duration)
+	numSamples := bufSize
+	_ = numSamples
+
+	// https://stackoverflow.com/questions/8299303/generating-sine-wave-sound-in-python
+	for i := range outBuf {
+		//k := i
+		// ?
+		k := float64(i)
+		// // increment = ((2 * PI) / SampleRate) * freq
+		//v := math.Sin(g.phase * 2.0 * math.Pi) // period of the wave is thus defined as: 2 * PI.
+		//v := math.Sin(phase)
+		// (amp * sin(2 * M_PI * m_sinePhase)) + 128;
+		//v := math.Sin(phase)
+		//v := math.Sin(2.0 * math.Pi * k * f/fs)
+		v := math.Sin(2.0 * math.Pi * k * phaseInc)
+		//samples[i][0] = v
+		//samples[i][1] = v
+		outBuf[i][0] = v
+		outBuf[i][1] = v
+		//_, g.phase = math.Modf(g.phase + g.sampleFactor)
+		//phase += phaseInc
+	}
+
+	// XXX ?
+	//phase = math.Modf(phase, 2.0 * math.Pi)
+
+	return outBuf
+}
+
+// Generate a noise buffer (?)
+//func GenNoiseBuf(sr int, scale float64, bufSize int) [][2]float64 {
+func GenNoiseBuf(scale float64, bufSize int) [][2]float64 {
+	//panic("not implemented")
+	
+	if scale > 1.0 || scale < 0 {
+		panic("bad scale?")
+	}
+	
+	// Out data ?
+	outBuf := make([][2]float64, bufSize)
+	
+	for i := range outBuf {
+		v := rand.Float64() * scale
+		
+		// Clamp ?
+		if v > 1.0 {
+			v = 1.0
+		}
+		if v < -1.0 {
+			v = -1.0
+		}
+		
+		outBuf[i][0] = v
+		outBuf[i][1] = v
+	}
+	
+	return outBuf
+}
+
+// Generate empty/silence buffer (?)
+// not tested
+func GenEmptyBuf(bufSize int) [][2]float64 {
+	// Out data ?
+	outBuf := make([][2]float64, bufSize)
+	
+	return outBuf
+}