Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I also stream the played sound to a file? #169

Open
dhairya0904 opened this issue Mar 7, 2024 · 3 comments
Open

Can I also stream the played sound to a file? #169

dhairya0904 opened this issue Mar 7, 2024 · 3 comments

Comments

@dhairya0904
Copy link

dhairya0904 commented Mar 7, 2024

Hi

I have a use case where I want to play the sound and also stream this to file to play it again.

package main

import (
	"log"
	"os"
	"time"

	"github.com/faiface/beep"
	"github.com/faiface/beep/mp3"
	"github.com/faiface/beep/speaker"
)

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()

	speaker.Init(48000, 2048)
	mixer := &beep.Mixer{}
	// done := make(chan bool)
	speaker.Play(mixer)

	mixer.Add(s1)
	mixer.Add(s2)

	time.Sleep(2 * time.Minute)
}

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
}

I want this sound to be saved in a file, such that I can play the entire stream again.
Can anyone please help me with this?
Any help is appreciated

@MarkKremer
Copy link
Contributor

Hi 👋 thank you for using Beep!

You can save audio to a file using the wav.Encode() function. Other encoding formats aren't supported currently.

Note that this repository is archived, but you can find our active fork over at https://github.com/gopxl/beep.

@dhairya0904
Copy link
Author

dhairya0904 commented Mar 9, 2024

@MarkKremer
I tried this but somehow the output file is too big.

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)

	outFile, err := os.Create("mixed_audio.wav")
	if err != nil {
		log.Fatal(err)
	}
	defer outFile.Close()

	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
}

@MarkKremer
Copy link
Contributor

Discussion continued in gopxl/beep#148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants