在很多方面,您的问题都类似于“有人可以向我解释如何捕鱼吗?”。有这么多方法和这么多可用的工具,每个答案虽然在技术上是正确的,但只是说明了回答的人的方式,从拖网渔船的工作人员到飞钓者到长矛钓者。
linux 上的音频就像狂野西部的水一样,“威士忌用于饮用,水用于争夺”。
只是为了好玩,请尝试以下链接以了解复杂性:
https://ubuntuforums.org/showthread.php?t=843012
http://alsa.opensrc.org/MultipleCards
但是给你一个可以从命令行运行的“音调”示例(并且可以写入代码、python 和 C 中)在你的机器上加载 gstreamer-1.0 并运行以下命令:
gst-launch-1.0 audiotestsrc freq=329.63 volume=0.5 ! autoaudiosink
gst-launch-1.0 audiotestsrc freq=987.77 ! autoaudiosink
gst-launch-1.0 audiotestsrc wave=2 freq=200 volume=0.2 ! tee name=t ! queue ! audioconvert ! autoaudiosink t. ! queue ! audioconvert ! libvisual_lv_scope ! videoconvert ! autovideosink
然后查看:
https://gstreamer.freedesktop.org/documentation/plugins.html
注意:gstreamer 只是飞钓者的故事,那是战斗的谈话!
这里有一些 Gtk 代码供你使用:
#!/usr/bin/env python
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, Gtk
class Tone(object):
def __init__(self):
window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
window.set_title("Tone-Player")
window.set_default_size(500, 200)
window.connect("destroy", Gtk.main_quit, "WM destroy")
vbox = Gtk.VBox()
window.add(vbox)
self.tone_entry = Gtk.Entry()
self.tone_entry.set_text('300.00')
vbox.pack_start(self.tone_entry, False, False, 0)
self.button = Gtk.Button("Start")
vbox.add(self.button)
self.button.connect("clicked", self.start_stop)
window.show_all()
self.player = Gst.Pipeline.new("player")
source = Gst.ElementFactory.make("audiotestsrc", "tone-source")
audioconv = Gst.ElementFactory.make("audioconvert", "converter")
audiosink = Gst.ElementFactory.make("autoaudiosink", "audio-output")
self.player.add(source)
self.player.add(audioconv)
self.player.add(audiosink)
source.link(audioconv)
audioconv.link(audiosink)
def start_stop(self, w):
if self.button.get_label() == "Start":
self.button.set_label("Stop")
tone = float(self.tone_entry.get_text())
self.player.get_by_name("tone-source").set_property("freq", tone)
self.player.set_state(Gst.State.PLAYING)
else:
self.player.set_state(Gst.State.NULL)
self.button.set_label("Start")
GObject.threads_init()
Gst.init(None)
Tone()
Gtk.main()