func main() { wavOut, err := os.Create("Test.wav") checkErr(err) defer wavOut.Close() meta := wav.File{ Channels: 1, SampleRate: rate, SignificantBits: bits, } writer, err := meta.NewWriter(wavOut) checkErr(err) defer writer.Close() start := time.Now() var freq float64 freq = 0.0001 for n := 0; n < 50*rate; n += 1 { y := int32(0.8 * math.Pow(2, bits-1) * math.Sin(freq*float64(n))) freq += 0.000002 err = writer.WriteInt32(y) checkErr(err) } fmt.Printf("Simulation Done. Took:%v\n", time.Since(start)) }
func (wr *WAVRecorder) Record() { var err error fmt.Println("*** Audio recording started") if wr.file, err = os.Create(fmt.Sprintf("audio.wav")); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) } meta := wav.File{ Channels: 1, SampleRate: 44100 * 2, SignificantBits: 16, } if wr.wavWriter, err = meta.NewWriter(wr.file); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) } }
// WriteSoundToWav creates a file at a path, and writes the given sound in the .wav format. func WriteSoundToWav(s sounds.Sound, path string) error { // Create file first, only if it doesn't exist: if _, err := os.Stat(path); err == nil { panic("File already exists! Please delete first") return os.ErrExist } file, err := os.Create(path) if err != nil { return err } defer func() { file.Close() }() // Create a .wav writer for the file var wf = wav.File{ SampleRate: uint32(sounds.CyclesPerSecond), Channels: 1, SignificantBits: 16, } writer, err := wf.NewWriter(file) if err != nil { return err } defer writer.Close() // Starts the sound, and accesses its sample stream. s.Start() samples := s.GetSamples() defer s.Stop() // Write a single sample at a time, as per the .wav writer API. b := make([]byte, 2) for sample := range samples { toNumber := uint16(sample * normScale) // Inverse the read scaling binary.LittleEndian.PutUint16(b, uint16(toNumber)) writer.WriteSample(b) } return nil }
func rebuildSound(centers []centroid, i uint64, bitrate int, samplerate int) { new_array := make([]int32, i, i) wav_out, err := os.Create("out.wav") defer wav_out.Close() meta := wav.File{ Channels: 1, SampleRate: uint32(samplerate), SignificantBits: uint16(bitrate / samplerate), } writer, err := meta.NewWriter(wav_out) defer writer.Close() checkErr(err) for q := 0; q < len(centers); q++ { for u := 0; u < len(centers[q].points); u++ { new_array[centers[q].points[u].t] = int32(centers[q].center.x) } } for p := range new_array { writer.WriteInt32(new_array[p]) } }