Golang : Play .WAV file from command line
Problem:
You have recorded your voice on a .wav
file with the previous tutorial on how to activate your microphone, record your voice and save the data into a .wav file from command line.
Now, you want to play back the .wav
file from command line.
How to do that?
Solution:
The approach is similar to how the voice in recorded with PlayAudio and encode with wave.Write()
function with the exception that this time is to read the .wav
file with wave.NewReader()
first.
Read the raw data with waveReader.ReadRawSample()
and transfer the data into a buffer before writing out to PortAudio.
Here you go!
playwav.go
package main
import (
"fmt"
"github.com/gordonklaus/portaudio"
wave "github.com/zenwerk/go-wave"
"math/rand"
"os"
"os/signal"
"time"
)
func errCheck(err error) {
if err != nil {
panic(err)
}
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage : %s <audiofilename.wav>\n", os.Args[0])
os.Exit(0)
}
audioFileName := os.Args[1]
fmt.Println("Playing [" + audioFileName + "] Press Ctrl-C to stop.")
waveReader, err := wave.NewReader(audioFileName)
errCheck(err)
// init PortAudio
portaudio.Initialize()
defer portaudio.Terminate()
inputChannels := 0
outputChannels := 1
sampleRate := 44100
framesPerBuffer := make([]byte, 1024)
stream, err := portaudio.OpenDefaultStream(inputChannels, outputChannels, float64(sampleRate), len(framesPerBuffer), &framesPerBuffer)
errCheck(err)
defer stream.Close()
// recording in progress ticker. From good old DOS days.
ticker := []string{
"-",
"\\",
"/",
"|",
}
rand.Seed(time.Now().UnixNano())
errCheck(stream.Start())
defer stream.Stop()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)
READERROR:
for {
// read into buffer to be played
//_, err := waveReader.Read(framesPerBuffer)
framesPerBuffer, err = waveReader.ReadRawSample()
if err != nil {
fmt.Println(err.Error())
break READERROR
}
errCheck(stream.Write())
fmt.Printf("\rPlaying now! [%v]", ticker[rand.Intn(len(ticker)-1)])
select {
case <-sig:
return
default:
}
}
if waveReader.NumSamples != waveReader.ReadSampleNum {
fmt.Printf("Actual samples : %d\nRead samples : %d\n", waveReader.NumSamples, waveReader.ReadSampleNum)
fmt.Println(waveReader.NumSamples, waveReader.ReadSampleNum)
} else {
fmt.Println("Wave file played without error")
}
}
References:
https://github.com/zenwerk/go-wave/blob/master/reader.go
http://people.csail.mit.edu/hubert/pyaudio/
https://socketloop.com/tutorials/golang-record-voice-audio-from-microphone-to-wav-file
See also : Golang : Record voice(audio) from microphone to .WAV file
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.7k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+10.6k Nginx : TLS 1.2 support
+23.6k Golang : Upload to S3 with official aws-sdk-go package
+9.4k Golang : interface - when and where to use examples
+13.7k Golang : Reverse IP address for reverse DNS lookup example
+9.1k Golang : How to protect your source code from client, hosting company or hacker?
+4.9k Golang : Print instead of building pyramids
+7.1k Golang : Accessing dataframe-go element by row, column and name example
+10.3k Golang : ISO8601 Duration Parser example
+27.1k PHP : Convert(cast) string to bigInt
+10.4k Golang : Flip coin example
+33.3k Golang : How to check if slice or array is empty?