简体   繁体   English

流式传输wav文件

[英]streaming wav files

I have a server that sends data via a socket, the data is a wav 'file'. 我有一个通过套接字发送数据的服务器,数据是一个wav'文件'。 I can easily write the data to disk and then play it in WMP, but I have no idea how I can play it as I read it from the socket. 我可以轻松地将数据写入磁盘,然后在WMP中播放,但我不知道如何在从套接字读取数据时播放它。 Is it possible? 可能吗?

Bonus question: how would I do it if the stream was in mp3 or other format? 奖金问题:如果流是mp3或其他格式,我该怎么做?

This is for windows in native C++. 这适用于本机C ++中的Windows。

Because you've said WMP, I'm assuming the question applies to trying to play a wav file on a windows machine. 因为您已经说过WMP,我假设这个问题适用于尝试在Windows机器上播放wav文件。 If not, this answer isn't relevant. 如果没有,这个答案是不相关的。

What you want to do isn't trivial. 你想做的不是微不足道的。 There is a good article here on code project that describes the windows audio model. 这是一个很好的文章在这里的代码项目,它描述了Windows音频模式。 It describes how to set up the audio device and how to stream data into the device for playback. 它介绍了如何设置音频设备以及如何将数据流式传输到设备中进行播放。 You "simply" need to supply data coming in from your socket as data for the playback buffers. 您“只需”提供来自套接字的数据作为回放缓冲区的数据。 But that's where all of the tricky work is. 但这就是所有棘手工作的地方。 You have to be sure that 你必须确定

  • You have enough data to begin a playback 您有足够的数据来开始播放
  • Handle the case when your socket is starved for data and you have nothing to send to the playback buffer 当您的套接字缺乏数据并且没有任何内容可以发送到回放缓冲区时处理这种情况
  • You are able to read data off of the socket with enough speed to keep the playback buffers full 您可以以足够的速度从套接字读取数据,以保持播放缓冲区满

It's an interesting exercise. 这是一项有趣的练习。 But tricky. 但是很棘手。

Mark is right about this being a tricky problem. 马克是对的,这是一个棘手的问题。 It may be less tricky if you use DirectSound instead of waveOut. 如果使用DirectSound而不是waveOut,可能不那么棘手。 Here's an article on streaming wave files from disk: streaming from the network is essentially the same process. 这是一篇关于从磁盘流式传输波形文件的文章 :来自网络的流媒体基本上是相同的过程。 Make sure you collect enough data from the network before you start - you'll want more than the 2 buffers the article mentions. 确保在开始之前从网络收集足够的数据 - 您需要的不仅仅是文章提到的2个缓冲区。

Even less tricky would be FMOD . FMOD甚至不那么棘手。 From the FAQ : 来自FAQ

Enhanced Internet features 增强的Internet功能

  • Internet audio streaming. 互联网音频流。 Custom internet streaming code is included, which allows for seamless SHOUTcast, Icecast and http streaming support. 包括自定义互联网流媒体代码,允许无缝SHOUTcast,Icecast和http流媒体支持。
  • Download capability. 下载功能。 A side effect of FMOD's modular file system which supports network files, even static samples can be loaded off the internet. FMOD的模块化文件系统的副作用支持网络文件,甚至静态样本也可以从互联网上加载。

File format support : FMOD currently supports a wide range of audio file formats. 文件格式支持 :FMOD目前支持多种音频文件格式。 partial list : 部分清单

  • MP3 - (MPEG I/II Layer 3, including VBR support) MP3 - (MPEG I / II第3层,包括VBR支持)
  • OGG - (Ogg Vorbis format) OGG - (Ogg Vorbis格式)
  • WAV - (Microsoft Wave files, inlcluding compressed wavs. PCM, MP3 and IMA ADPCM compressed wav WAV - (Microsoft Wave文件,包括压缩的wavs。PCM,MP3和IMA ADPCM压缩的wav

Mark is right about this being a tricky problem. 马克是对的,这是一个棘手的问题。 The waveOutXXXX API is ancient (it predates Windows 95) and requires more error-prone coding than you would think. waveOutXXXX API很古老(它早于Windows 95),并且需要比您想象的更容易出错的编码。 You will have an easier time interacting with the API in C++ than with C#. 与C#相比,您可以更轻松地在C ++中与API进行交互。 Just make sure this is something you really want to do. 只要确保这是你真正想做的事情。

If your stream is some format other than WAV file data (like MP3 or WMA), you will have to perform the additional step of decoding the data into WAV format and playing it with the waveOutXXXX API. 如果您的流是WAV文件数据以外的某种格式(如MP3或WMA),则必须执行将数据解码为WAV格式并使用waveOutXXXX API播放的附加步骤。 Finding a good component to do MP3 decoding is trickier than you would expect - I think this is related to the Fraunhofer licensing situation (you're supposed to pay them if you use MP3 code in any way). 找到一个好的组件进行MP3解码比你想象的要复杂 - 我认为这与Fraunhofer授权情况有关(如果你以任何方式使用MP3代码,你应该支付它们)。

I'd find an off-the-shelf product to do this, unless you want the learning experience. 除非你想要学习经验,否则我会找到一个现成的产品来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM