I am using this module for playing sound in my project - https://github.com/faiface/beep I want to play the sound and stream that played sound to a file or a http endpoint.
I am adding streams to mixer like this:
soundMixer.Add(stream)
speaker.Play(s.soundMixer)
Can anyone please help me with this? Any help is appreciated
Exact code where I want this change - https://github.com/ikemen-engine/Ikemen-GO/blob/9fcfb350fc5a1255ab74d264e96b773187e8ad71/src/sound.go#L222-L225
Sample Code
package main
import (
"log"
"os"
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/wav"
)
func main() {
s1 := getStream("./Lame_Drivers_-_01_-_Frozen_Egg.mp3")
// s2 := getStream("./Miami_Slice_-_04_-_Step_Into_Me.mp3")
defer s1.Close()
// defer s2.Close()
mixer := &beep.Mixer{}
mixer.Add(s1)
// mixer.Add(s2)
// Instead of initializing the speaker, open a file to save the mixed audio
outFile, err := os.Create("mixed_audio.wav")
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
// Assuming a sample rate of 48000 for the output file, but you should use the sample rate of your input files if they are different
err = wav.Encode(outFile, mixer, beep.Format{SampleRate: 48000, NumChannels: 2, Precision: 2})
if err != nil {
log.Fatal(err)
}
}
func getStream(file string) beep.StreamSeekCloser {
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
streamer, _, err := mp3.Decode(f)
if err != nil {
log.Fatal(err)
}
return streamer
}
wav.enode does help in saving the sound to a file but the file is too large(50G). It streams the complete sound file at once and probably streams silence to the file. How can I fix this?
Your programm finished before sounds are played.