Comments (3)
The example at demo/record shows how to record audio to a .wav File. To encode as ogg you would need a separate encoding library.
from pulse.
This is my code
// play -t raw -r 48000 -e signed -b 16 -c 1 test.raw
//
package main
import (
"encoding/binary"
"fmt"
"os"
"os/signal"
"io/ioutil"
"github.com/gordonklaus/portaudio"
"gopkg.in/hraban/opus.v2"
)
func main() {
var readBuffer = make([]byte, 0)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)
fileName := "test.raw"
f, err := os.Create(fileName)
chk(err)
portaudio.Initialize()
defer portaudio.Terminate()
in := make([]int16, 960)
stream, err := portaudio.OpenDefaultStream(1, 0, 48000, len(in), in)
chk(err)
defer stream.Close()
chk(stream.Start())
for {
chk(stream.Read())
chk(binary.Write(f, binary.LittleEndian, in))
buf, encErr := encode(in)
if err != nil {
panic(encErr)
}
readBuffer = append(readBuffer, buf...)
//fmt.Println(in, len(in))
select {
case <-sig:
// write the whole body at once
err = ioutil.WriteFile("output.ogg", readBuffer, 0644)
if err != nil {
panic(err)
}
return
default:
}
}
chk(stream.Stop())
}
func encode(pcm []int16) ([]byte, error){
const sampleRate = 48000
const channels = 1 // mono; 2 for stereo
const bufferSize = 1000 // choose any buffer size you like. 1k is plenty.
enc, err := opus.NewEncoder(sampleRate, channels, opus.AppVoIP)
if err != nil {
panic(err)
}
// Check the frame size. You don't need to do this if you trust your input.
frameSize := len(pcm) // must be interleaved if stereo
frameSizeMs := float32(frameSize) / channels * 1000 / sampleRate
switch frameSizeMs {
case 2.5, 5, 10, 20, 40, 60:
// Good.
default:
fmt.Println("Illegal frame size: ", frameSize, "bytes", frameSizeMs)
return nil, err
}
data := make([]byte, bufferSize)
n, err := enc.Encode(pcm, data)
if err != nil {
panic(err)
}
data = data[:n] // only the first N bytes are opus data. Just like io.Reader.
fmt.Print(n, len(data))
return data, err
}
func chk(err error) {
if err != nil {
panic(err)
}
}
but when i try play output.ogg with mplayer.ogg i receive this message
MPlayer 1.3.0 (Debian), built with gcc-7 (C) 2000-2016 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
Playing output.ogg.
libavformat version 57.83.100 (external)
libavformat file format detected.
[ogg @ 0xb6952e98]cannot find sync word
LAVF_header: av_open_input_stream() failed
libavformat file format detected.
[ogg @ 0xb6952e98]cannot find sync word
LAVF_header: av_open_input_stream() failed
from pulse.
I'm sorry, I don't see how your question is at all related to this repository, since you use a different library for audio input and the issue seems to be mainly about encoding.
from pulse.
Related Issues (14)
- Please support GetPlaybackLatency and GetRecordLatency HOT 1
- Please support a PlaybackOption for prebuffer length HOT 2
- Consider making some fields exported to allow mixing high and low level interactions HOT 3
- Cannot use RawRequest with GetPlaybackLatency HOT 3
- Short writes to a playback stream can cause high CPU consumption HOT 3
- Getting sink info with invalid index renders client useless
- Slice bounds out of range while parsing a prop list HOT 2
- playback goroutine leak HOT 1
- Feature: TCP socket support HOT 1
- Send on closed channel panic on RecordStream.Close() after error HOT 1
- Structs Source && Sync have unexported field
- Tag a release? HOT 2
- Optionally use $PULSE_SERVER to find pulse socket HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pulse.