通过socket接收音频文件
我正在尝试从 C 套接字接收文件(音频、.CAF)(C++ 解决方案也可以)。我已经使用字符串对其进行了测试,使套接字通信正常工作。我的问题是我不知道要向 recv(socket, buffer, buffer_size, 0) 中的第二个参数提供什么。我应该制作什么类型的“缓冲区”?我基本上想接收一个音频文件,然后播放它。但不知道如何接收音频文件本身。
有什么想法吗?
谢谢,
罗宾
I am trying to receive a file (audio, .CAF) from a socket in C (C++ solution ok as well). I have the socket communication working, having tested it with strings. My problem is I don't know what to supply to the 2nd arg in recv(socket, buffer, buffer_size, 0). What type should I make "buffer"? I basically want to receive an audio file, and will then play it. But don't know how to receive the audio file itself.
Any thoughts?
Thanks,
Robin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您将以某种格式对音频进行编码。为了简单起见,我们假设它是 Wave 格式。
一种方法是封装音频块(例如 50 毫秒的块)以便通过网络发送。
但是,您不能盲目地通过网络发送数据并期望它起作用。在您的计算机上,数据可能以一种方式组织(小端或大端),而在另一台计算机上则可能以相反的方式组织。
在这种情况下,客户将获得他所解释的数据,这些数据与您的预期完全不同。因此,您需要以某种方式正确序列化它。
一旦你正确地序列化了数据(或者不序列化,如果两台计算机使用相同的字节顺序!),你可以直接 send() 和 rcev() ,然后将其传递给解码器来处理。
我很乐意提供更多信息,但这就是我对此主题的了解程度。这实际上取决于您到底在做什么,因此如果没有关于您正在做什么的更多细节(例如,关于音频格式),就很难提供更多信息。
更多信息:
http://en.wikipedia.org/wiki/Serialization
http://en.wikipedia.org/wiki/Endianness
编辑:正如本答案的评论所指出的,如果您使用标准格式,您可能根本不必担心序列化。只需将其以解码器可用的块形式通过网络传递(例如,一次发送一帧),然后在客户端解码这些单独的帧(或可能是多个帧)。
Typically, you'll have the audio encoded in some format. For simplicity, let's assume it's Wave format.
One way of doing things would be to encapsulate chunks of the audio (Say, 50 ms chunks) for sending over the network.
However, you can't blindly send data over the network and expect it to work. On your computer, data may be organized one way (little or big endian), and it could be organized in the opposite way on the other computer.
In that case, the client will get data that he interprets as being completely different than what you intended. So, you'll need to properly serialize it somehow.
Once you properly serialize the data though (or not, if both computers use the same endianess!), you can just send() it and rcev() it, then just pass it off to a decoder to deal with.
I'd love to offer more information, but that's about the extent of my knowledge on the subject. It really depends on what exactly you're doing, so it's hard to give any more information without some more specifics as to what you're doing (with regards to audio format, for one).
Some more information:
http://en.wikipedia.org/wiki/Serialization
http://en.wikipedia.org/wiki/Endianness
Edit: As pointed out in the comments of this answer, you should probably not worry about serialization at all if you're using a standard format. Just pass it over the network in chunks that are usable by your decoder (Send a frame at a time, for example) then decode those individual frames (or possibly multiple frames) on the client side.
buffer
将是一个指向数组的指针,您已分配该数组来存储通过线路传输的数据。它取决于您使用的套接字库,但通常需要
void*
(这只是一个通用指针类型)。你可能会这样做:
这会变得很棘手,因为这只为你提供了 8,000 字节的足够空间,这可能不足以容纳你的音频文件,因此你必须处理多个 receive() 调用,直到获得整个音频文件。
buffer
is going to be a pointer to an array you've allocated to store the data the comes across the wire.It depends on the socket library you're using, but usually it expects
void*
(which is just a generic pointer type).You might do something like this:
It gets tricky because this only gives you enough room for 8,000bytes, which might not be enough to hold your audio file, so you'll have to handle multiple recv() calls until you get the entire audio file.