func main() { bitrate := 705600 // get from the file samplerate := 44100 // get from the file info, err := os.Stat("good_times.wav") Wav, err := os.Open("good_times.wav") wavReader, err := wav.NewReader(Wav, info.Size()) checkErr(err) var data []sample i := uint64(0) Loop: for { s, err := wavReader.ReadSample() if err == io.EOF { break Loop } else if err != nil { panic(err) } data = append(data, sample{x: float64(s), t: i}) i = i + 1 } rebuildSound(kmeans(data, 50), i, bitrate, samplerate) }
func main() { if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "Usage: simpleRead <file.wav>\n") os.Exit(1) } testInfo, err := os.Stat(os.Args[1]) checkErr(err) testWav, err := os.Open(os.Args[1]) checkErr(err) wavReader, err := wav.NewReader(testWav, testInfo.Size()) checkErr(err) fmt.Println("Hello, wav") fmt.Println(wavReader) sampleLoop: for { s, err := wavReader.ReadRawSample() if err == io.EOF { break sampleLoop } else if err != nil { panic(err) } fmt.Printf("Sample: <%v>\n", s) } }
func main() { if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "Usage: plotWav <file.wav>\n") os.Exit(1) } // open file testInfo, err := os.Stat(os.Args[1]) checkErr(err) testWav, err := os.Open(os.Args[1]) checkErr(err) wavReader, err := wav.NewReader(testWav, testInfo.Size()) checkErr(err) // File informations fmt.Println(wavReader) // limit sample count sampleCnt := wavReader.GetSampleCount() if sampleCnt > 10000 { sampleCnt = 10000 } // setup plotter p, err := plot.New() checkErr(err) p.Title.Text = "Waveplot" p.X.Label.Text = "t" p.Y.Label.Text = "Ampl" pts := make(plotter.XYs, sampleCnt) // read samples and construct points for plot for i := range pts { n, err := wavReader.ReadSample() if err == io.EOF { break } checkErr(err) pts[i].X = float64(i) pts[i].Y = float64(n) } err = plotutil.AddLinePoints(p, "", pts) checkErr(err) // construct output filename inputFname := path.Base(os.Args[1]) plotFname := strings.Split(inputFname, ".")[0] + ".pdf" if err := p.Save(10*vg.Inch, 4*vg.Inch, plotFname); err != nil { panic(err) } }
// loadWavReaderOrPanic reads a wav file and handles failure cases. func loadWavReaderOrPanic(path string) *wav.Reader { testInfo, err := os.Stat(path) if err != nil { panic(err) } testWav, err := os.Open(path) if err != nil { panic(err) } result, err := wav.NewReader(testWav, testInfo.Size()) if err != nil { panic(err) } return result }