Coder Social home page Coder Social logo

get live stream from microphone about pulse HOT 3 CLOSED

jfreymuth avatar jfreymuth commented on August 26, 2024
get live stream from microphone

from pulse.

Comments (3)

jfreymuth avatar jfreymuth commented on August 26, 2024

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.

projetoarduino avatar projetoarduino commented on August 26, 2024

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.

jfreymuth avatar jfreymuth commented on August 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.