Audio in golang

polaris · · 2173 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve been trying to find some way to stream audio with golang for like a week now and I&#39;m going crazy! The <a href="https://github.com/gordonklaus/portaudio" rel="nofollow">portaudio</a> package seemed useful, but I could not find any single way to actually make it read the mp3 or wav streams I had. I&#39;m lost and I don&#39;t know how to progress. Anyone have any thoughts?</p> <hr/>**评论:**<br/><br/>faiface: <pre><p>Hi! I&#39;ve got a pretty cool audio library in the works, but it&#39;s fairly usable already:</p> <p><a href="https://github.com/faiface/beep" rel="nofollow">https://github.com/faiface/beep</a></p> <p>MP3 support is coming like today, or tomorrow. You can do streaming, audio processing, easily create your own effects, sequencing, mixing, and so on. The tutorial is pretty short so far, but that&#39;s because there hasn&#39;t been an official release yet, but that&#39;s coming (the docs should be fine). If you have any questions, please ask.</p></pre>ohaiyou_meepo_desu: <pre><p>This would be awesome! Cannot wait for the mp3 support! Also consider listing your package on <a href="https://go.libhunt.com/contribute" rel="nofollow">https://go.libhunt.com/contribute</a>, it&#39;s so hard to find audio packages that aren&#39;t dead for Go.</p></pre>Sythe2o0: <pre><p>Awesome-go has prerequisites for hosting code bases, one of which is that they have significant (at least 80-90%) test coverage, so they might not be posting it there because they haven&#39;t reached that yet, or otherwise they want it to be in a finished state first.</p></pre>faiface: <pre><p>My Pixel library is in awesome-go and has like zero tests (but is tested manually very well). The main reason why beep isn&#39;t in awesome-go yet is that it hasn&#39;t been released yet, because a few features are still missing (such as a Resample decorator/&#34;effect&#34;, help appreciated).</p></pre>faiface: <pre><p>MP3 support done (uses awesome <a href="https://github.com/hajimehoshi/go-mp3" rel="nofollow">https://github.com/hajimehoshi/go-mp3</a> under the hood). Btw, do you want to stream audio through a server, or do you want to play it on your computer?</p></pre>ohaiyou_meepo_desu: <pre><p>Play it on the computer. And awesome! Gunna go check it out now.</p></pre>faiface: <pre><p>Cool! Let me know how you like it and whether you encountered any problems.</p></pre>ohaiyou_meepo_desu: <pre><p>Found an odd one with the mp3 decoder; opened a ticket on the repo :)</p></pre>faiface: <pre><p>Thanks, I&#39;ll take a look asap when I get home. It seems to be a problem with go-mp3 I guess.</p></pre>Sythe2o0: <pre><p>There are a handful of wav decoders out there. You should be able to find them easily enough. Most of them will fail to decode some specific wav files that don&#39;t use a 44 byte header, because those files have a different format entirely. I&#39;ve been working on a RIFF decoding format to deal with both of those cases, but it isn&#39;t ready.</p> <p>There are fewer mp3 decoders, but <a href="https://github.com/hajimehoshi/go-mp3" rel="nofollow">go-mp3</a> is one I&#39;ve found to be stable.</p> <p>The portaudio library is pretty low level, I haven&#39;t worked with it specifically though. If you want to use it and have error messages or code you could put to a playground link to look at, I could try to dig through it.</p> <p>And I guess we&#39;re plugging our audio libraries, so <a href="https://github.com/200sc/klangsynthese" rel="nofollow">this over here</a> is the one I&#39;ve been working on, that contains that RIFF work.</p></pre>ohaiyou_meepo_desu: <pre><p>I&#39;ve looked into go-mp3, but it can only handle local filesystem files. Though I&#39;m sure if I had to use it I could fork and fix that, but I wanna explore the solutions in this thread first.</p> <blockquote> <p>If you want to use it and have error messages or code you could put to a playground link to look at, I could try to dig through it.</p> </blockquote> <p>Not particularly error messages. I have a stream of an mp3 file, I have a portaudio stream. I don&#39;t know what to do with the two, is what my issue is.</p> <p>As far as your library goes, it seems pretty nice. I see a ? next to linux playback, what&#39;s that?</p></pre>Sythe2o0: <pre><p>Linux playback works for simple, one-time plays, but it appears that playing things rapidly doesn&#39;t play nice with the lower-level libraries. It&#39;s a little rough to test this because my laptop doesn&#39;t play nice with linux VMs and you can&#39;t test audio through automated tools like Travis CI, because those machines don&#39;t have audio cards.</p> <p>Klangsynthese also uses go-mp3 under the hood, so both libraries proposed in this thread so far are directing you the same way, sorry.</p></pre>faiface: <pre><p>Beep uses <a href="https://github.com/hajimehoshi/oto" rel="nofollow">https://github.com/hajimehoshi/oto</a> for playback, which is a collaborative effort with Hajime Hoshi. Recently I implemented ALSA backend for Linux for oto and that enabled really low-latency playback. Feel free to to do something similar for your library. We&#39;re working on such low latency for other platforms too.</p></pre>Sythe2o0: <pre><p>Klang uses ALSA, through an old library called alsa-go. I have a different pattern for playback than the one used by oto. It has low-latency playback, it&#39;s just that there&#39;s probably some maximum number of players that we aren&#39;t reusing or something on ALSA&#39;s end.</p> <p>I&#39;m not in love with oto&#39;s playback because if you want to play multiple types of audio with different bit rates or sample rates you have to get a new player to do it. The pattern used by Klang attaches the encoding information to each audio sample, so they each act as their own player. That pattern of each audio acting as its own player causes the mentioned bug, as far as I can see.</p> <p>The same bug should be present in oto if you try to play multiple files very fast, because files need to each have their own player so you can make sure they are using the right encoding, or otherwise you&#39;re checking to see if you already have a player of that encoding type.</p></pre>faiface: <pre><p>Beep implements its own mixing, so it can play arbitrary number of streamers simultaneously (as long as your CPU can handle it). It only ever uses one ALSA device/player.</p></pre>Sythe2o0: <pre><p>What does beep do when presented with something of a different sample rate or bit rate?</p></pre>faiface: <pre><p>You set the sample rate of the speaker package and then everything you play through the speaker is assumed to have that sample rate. If it doesn&#39;t, you need to resample manually, which will be available by the (yet unimplemented) Resample decorator.</p></pre>Sythe2o0: <pre><p>It also looks like you cast all audio to be stereo and 16 bit? Or at least the player assumes that&#39;s what it&#39;ll be playing?</p></pre>faiface: <pre><p>Take a look at the core Streamer interface. In the audio processing stage (outside of files and playback backends) everything is stereo float64. Then speaker package converts that to its internal format when playing, which is 16bit stereo, but it doesn&#39;t really matter.</p></pre>guitmz: <pre><p>you could use gstreamer or <a href="https://github.com/hajimehoshi/go-mp3" rel="nofollow">https://github.com/hajimehoshi/go-mp3</a> or <a href="https://github.com/badgerodon/mp3" rel="nofollow">https://github.com/badgerodon/mp3</a> for example</p></pre>ohaiyou_meepo_desu: <pre><p>AFAIK gstreamer is linux-only. I&#39;ve looked into go-mp3, but it can only handle local filesystem files. Though I&#39;m sure if I had to use it I could fork and fix that, but I wanna explore the solutions in this thread first.</p></pre>faiface: <pre><p>go-mp3 can handle any io.Reader, which doesn&#39;t have to be a local filesystem file.</p></pre>guitmz: <pre><p>Gstreamer is Linux but you can use windows api to play audio too if you are on Windows. You can implement both and have your app to decide at runtime too</p></pre>shazow: <pre><p>I used <a href="https://github.com/mjibson/moggio" rel="nofollow">moggio</a> for some projects with audio which supports mp3 and wav decoding, and has cross-platform audio abstractions, among other things.</p></pre>mjibson: <pre><p>I&#39;m the moggio author. I had no idea anyone was using it for anything. It was written some years ago before most of the above packages existed, and would maybe be done differently now. But it does play vorbis, flac, wav, mp3 on linux, mac, windows, which is pretty good.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2173 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传