go-wav
go-wav copied to clipboard
can you provide an example about creating a wave file?
Hi,
I'm struggling to create a wave file, I tried this simple program, but it doesn't sound right! (pardon my comicity )
file, err := os.OpenFile("./t.wav", os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
}
// 8BitStereo
var numSamples uint32 = 10
var numChannels uint16 = 2
var sampleRate uint32 = 44100
var bitsPerSample uint16 = 8
writer := NewWriter(file, numSamples, numChannels, sampleRate, bitsPerSample)
samples := make([]Sample, numSamples)
samples[0].Values[0] = 255
samples[0].Values[1] = 0
samples[1].Values[0] = 123
samples[1].Values[1] = 234
samples[2].Values[0] = 255
samples[2].Values[1] = 0
samples[3].Values[0] = 123
samples[3].Values[1] = 234
samples[4].Values[0] = 255
samples[4].Values[1] = 0
samples[5].Values[0] = 123
samples[5].Values[1] = 234
samples[6].Values[0] = 255
samples[6].Values[1] = 0
samples[7].Values[0] = 123
samples[7].Values[1] = 234
samples[8].Values[0] = 255
samples[8].Values[1] = 0
samples[9].Values[0] = 123
samples[9].Values[1] = 234
err = writer.WriteSamples(samples)
if err != nil {
fmt.Println(err)
}
file.Close()
On mac, if I play I just hear a quick bump.
play t.wav
t.wav:
File Size: 48 Bit Rate: 8.47M
Encoding: Unsigned PCM
Channels: 2 @ 8-bit
Samplerate: 44100Hz
Replaygain: off
Duration: 00:00:00.00
In:100% 00:00:00.00 [00:00:00.00] Out:2 [!=====|=====!] Hd:0.0 Clip:0
Done.
is the file right, but I didn't specified any sound duration?
Help!
That's all fine. The duration of your file is 10/44100 seconds.
Any update on this? I have a slice of audio bytes data, have no idea how to create a wave file from it.. any example?
Example above works. The only trouble is there is too short data. See my previous post.
@edwvee,
thanks for the reply, but how to correctly change the type []byte of audio data to type []Sample? I saw the Sample type its Values field is array of 2 int. Sorry, I not knowing much in digital audio..
I'll try to find my examples. That was too long ago. I guess two values because it's stereo. Try to read a known wav and recreate it while debugging. To convert just cast byte to int:
type Sample struct {
Values [2]int
}