You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.1 KiB

package main
import (
)
import (
"os"
"fmt"
"github.com/mjibson/go-dsp/wav"
"github.com/mjibson/go-dsp/dsputils"
"github.com/mjibson/go-dsp/fft"
"github.com/mjibson/go-dsp/window"
"math"
)
var spec_framerate = 144
var fft_length int = 1024
var display_length = 32
var freq_varying_offset float64 = float64(fft_length) * 0.000005
func main() {
//fmt.Println(fft.FFTReal([]float64 {1, 2, 3}))
r, err := os.Open("./file.wav")
if err != nil {
fmt.Println("File open error: ", err)
return
}
wr, err := wav.New(r)
if err != nil {
fmt.Println("Wav read error: ", err)
return
}
fmt.Printf("WAV:%+v\n", wr)
//fmt.Println("Frame size: ", frame_size)
var floats []float32
for i := 0; i < 4000; i++ {
floats, err = wr.ReadFloats(fft_length * int(wr.NumChannels))
}
if err != nil {
fmt.Println("Error reading samples: ", err)
}
//fmt.Println("Floats!: ", floats)
upgraded := averageChannels(floats, int(wr.NumChannels))
window.Apply(upgraded, window.Hann)
c := dsputils.ToComplex(upgraded)
ifs := fft.IFFT(c)
//fmt.Println("IFFT: ", ifs[1:])
mag := make([]float64, display_length)
var total float64 = 0.0
counter := 0
for _, n := range ifs[1:display_length + 1] {
mag[counter] = math.Sqrt(real(n)*real(n) + imag(n)*imag(n))
fmt.Printf("\nMag:%+v\n", mag[counter])
total += mag[counter]
counter++
}
for i, n := range mag {
z := int(math.Floor((n / freq_varying_offset - 0.02) * 10))
fmt.Printf("\n%dHz:\t\t|", wr.SampleRate/uint32(fft_length)*uint32(i))
for i := 0; i < z; i++ {
fmt.Print("X")
}
}
fmt.Println("\nTotal: ", total)
}
func averageChannels(input []float32, channels int) []float64 {
//the length of input should always be a multiple of channels
counter := 0
output := make([]float64, len(input)/channels)
for i := 0; i < len(input); i+= channels {
var average float64 = 0.0
for _, n := range input[i:i+channels] {
average += float64(n)
}
output[counter] = average/float64(channels)
counter++
}
//fmt.Printf("Ouput:%+v\n%d", output, len(output))
//fmt.Printf("Length of input:%d\nLength of output:%d\nChannels:%d", len(input), len(output), channels)
return output
}