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

transfer pcm to opus #15

Closed
likezjuisee opened this issue Sep 21, 2017 · 2 comments · Fixed by #44
Closed

transfer pcm to opus #15

likezjuisee opened this issue Sep 21, 2017 · 2 comments · Fixed by #44

Comments

@likezjuisee
Copy link

likezjuisee commented Sep 21, 2017

The pcm data I got is "[]byte" format, so how can I transfer it to "int16", and then encode to opus?
I have tried the code as below, but the output file is not opus file, and can't play.

func writeUSBDataToSamples(usbData []byte) []wav.Sample {
samples := make([]wav.Sample, 0)
for i := 0; i < len(usbData); i += 2 {
sample := wav.Sample{}
sample.Values[0] = int(usbData[i])
sample.Values[0] = int(usbData[i+1])
samples = append(samples, sample)
}
return samples
}

opusOutput := make([]byte, 0)
opusData := make([]byte, 4096)

encodedNumSum := 0
for encodedNumSum+OpusFrameSize < len(pcmInt16Data) {
	encodedNum, err := enc.Encode(pcmInt16Data[encodedNumSum:(encodedNumSum+OpusFrameSize)], opusData)
	if err != nil {
		fmt.Println("enc.Encode err ", err.Error())
		break
	}
	fmt.Println("encodedNum ", encodedNum)
	opusOutput = append(opusOutput, opusData[:encodedNum]...)
	encodedNumSum += OpusFrameSize
}
fmt.Println("opusOutput length ", len(opusOutput))
fopus, _ := os.OpenFile("output.opus", os.O_RDWR|os.O_CREATE, 0777)
fopus.Write(opusOutput)
fopus.Close()
@hraban
Copy link
Owner

hraban commented Sep 21, 2017

There is a difference between an opus stream (which is an ogg/opus stream) and raw opus data. .opus files are really .ogg files with opus data, which this package doesn't provide a way to write. It does have a reader for it (Stream type), but no writer yet (never bothered to because I haven't needed it so far, but feel free to submit a PR).

@likezjuisee
Copy link
Author

Thank you, I got it.

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

Successfully merging a pull request may close this issue.

2 participants