从操作系统进行音频流传输

huangapple go评论125阅读模式
英文:

Audio Streaming from OS

问题

我想要与我的计算机音频输出进行接口,并使用FFT生成音频的可视化效果。

我的问题是:“我应该从哪里获取计算机的音频输出流?有没有用于此目的的有用库?”我查看的所有示例都是从文件流中获取音频,对我来说并不是很有用。

我希望在Golang和Linux环境下进行开发。

英文:

I would like to interface with the output of my computer's audio and generate a visualization of that audio with fft's.

My question is "Where do I get the audio output stream of my computer? Are there any useful libraries for this purpose?" All the examples I've looked at stream from files, which isn't very useful to me.

I'm hoping to work in golang and linux.

答案1

得分: 3

请查看http://go-lang.cat-v.org/library-bindings中的“图形和音频”以及“音频”部分。

特别是与PortAudio(http://code.google.com/p/portaudio-go/)和PulseAudio(https://github.com/moriyoshi/pulsego/)相关的绑定可能对您有用,因为您是一个Linux用户。

英文:

Have a look at the "Graphics and Audio" and "Audio" sections of http://go-lang.cat-v.org/library-bindings.

Especially the bindings to PortAudio (http://code.google.com/p/portaudio-go/) and PulseAudio (https://github.com/moriyoshi/pulsego/) could be of use to you, being a linux guy.

答案2

得分: 2

使用golang播放声音,您可以使用beep库:http://github.com/faiface/beep ,请参阅教程

package main

import (
	"log"
	"os"
	"time"

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

func main() {
	f, err := os.Open("../Lame_Drivers_-_01_-_Frozen_Egg.mp3")
	if err != nil {
		log.Fatal(err)
	}

	streamer, format, err := mp3.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	defer streamer.Close()

	speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))

	done := make(chan bool)
	speaker.Play(beep.Seq(streamer, beep.Callback(func() {
		done <- true
	})))

	<-done
}

要录制计算机的音频(麦克风),您可以尝试使用这个教程:https://medium.com/@valentijnnieman_79984/how-to-build-an-audio-streaming-server-in-go-part-1-1676eed93021 ,它使用了PortAudio绑定:

package main
import (
	"encoding/binary"
	"github.com/gordonklaus/portaudio"
	"net/http"
)

const sampleRate = 44100
const seconds = 1

func main() {
	portaudio.Initialize()
	defer portaudio.Terminate()
	buffer := make([]float32, sampleRate * seconds)
	stream, err := portaudio.OpenDefaultStream(1, 0, sampleRate, len(buffer), func(in []float32) {
		for i := range buffer {
			buffer[i] = in[i]
		}
	})

	if err != nil {
		panic(err)
	}

	stream.Start()
	defer stream.Close()
}
英文:

For playing back sound with golang you can use beep: http://github.com/faiface/beep , see the tutorial:

package main

import (
	&quot;log&quot;
	&quot;os&quot;
	&quot;time&quot;

	&quot;github.com/faiface/beep&quot;
	&quot;github.com/faiface/beep/mp3&quot;
	&quot;github.com/faiface/beep/speaker&quot;
)

func main() {
	f, err := os.Open(&quot;../Lame_Drivers_-_01_-_Frozen_Egg.mp3&quot;)
	if err != nil {
		log.Fatal(err)
	}

	streamer, format, err := mp3.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	defer streamer.Close()

	speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))

	done := make(chan bool)
	speaker.Play(beep.Seq(streamer, beep.Callback(func() {
		done &lt;- true
	})))

	&lt;-done
}

For recording your computers audio (microphone), you could try this tutorial: https://medium.com/@valentijnnieman_79984/how-to-build-an-audio-streaming-server-in-go-part-1-1676eed93021 that uses the PortAudio bindings:

package main
import (
 &quot;encoding/binary&quot;
 &quot;github.com/gordonklaus/portaudio&quot;
 &quot;net/http&quot;
)

const sampleRate = 44100
const seconds = 1

func main() {
 portaudio.Initialize()
 defer portaudio.Terminate()
 buffer := make([]float32, sampleRate * seconds)
 stream, err := portaudio.OpenDefaultStream(1, 0, sampleRate,   len(buffer), func(in []float32) {
  for i := range buffer {
   buffer[i] = in[i]
  }
 })

 if err != nil {
   panic(err)
 }

 stream.Start()
 defer stream.Close()
}

答案3

得分: 0

我知道这是一段时间前的内容,但如果还有其他人有同样的疑问,我一直在研究这个:https://github.com/padster/go-sound

声音被建模为浮点样本的通道(44.1khz,每个样本的范围为[-1, 1]),你可以对它们进行处理,例如播放到扬声器(目前通过pulsego实现),将它们写入文件或使用OpenGL在屏幕上显示。

还有一些实验性的FFT代码(类似于常量Q)。

英文:

I know this is from a while ago, but if anyone else is wondering the same, I've been working on: https://github.com/padster/go-sound

Sounds are modelled as channels of float samples (44.1khz, each sample in the range [-1, 1]), and you can process them, or e.g. play to speakers (currently via pulsego), write them to file or display on screen using openGL.

There's also some experimental FFT code (Constant Q, which is similar)

huangapple
  • 本文由 发表于 2015年3月19日 06:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/29134214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定