使用本机系统调用在C ++中播放声音?

时间:2022-09-01 23:21:40

So in Windows, you can use the PlaySound function to play an MP3 file in a C++ application. What would be some similar functions available for use in Mac OS X/Linux? I would also appreciate it if you could link to some sample Hello-World type programs demonstrating this.

因此在Windows中,您可以使用PlaySound功能在C ++应用程序中播放MP3文件。在Mac OS X / Linux中可以使用哪些类似的功能?如果您可以链接到一些示例Hello-World类型的程序,我也将不胜感激。

3 个解决方案

#1


4  

You can just open("/dev/dsp") and write to it. That's as "native syscall" as you can get, I believe.

你可以打开(“/ dev / dsp”)并写入它。我相信,这就像你可以获得的“原生系统调用”一样。

#2


3  

Concerning Audio: Linux != Mac, so I will answer both separately.

关于音频:Linux!= Mac,所以我会单独回答。

MacOS uses the core audio framework for low level audio. Building on that there are several higher level APIs. If you just want to play a sound file the AV Foundation Framework is most likely the way to go. Here you find an example to build a simple app for iOS, which can be ported to MacOS easily.

MacOS将核心音频框架用于低级音频。在此基础上,有几个更高级别的API。如果您只想播放声音文件,AV Foundation Framework很可能是最佳选择。在这里,您可以找到为iOS构建简单应用程序的示例,该应用程序可以轻松移植到MacOS。

For Linux the whole situation is quite delicate. There exist many different frameworks and libraries and it very unclear which is low and which is high level, since some of them are emulating the others. Basically there is ALSA and OSS for low level audio. Both of them are crap. Jack is aiming to provide a framework for professional audio but it's not very portable (at least if you are interested in embedded devices). Most Linux OS currently are using PulseAudio as their audio server, which has also a simple API for just playing a single sound and a very difficult API for low level stuff. And then there is the gstreamer library, which is rather portable and has a half way descent API and is especially designed for playing audio & video files and streams, and so probably the way to go on linux.

对于Linux来说,整个情况非常微妙。存在许多不同的框架和库,并且非常不清楚哪个是低的并且是高级的,因为其中一些正在模仿其他的。基本上ALSA和OSS用于低级音频。他们俩都是垃圾。 Jack的目标是提供专业音频的框架,但它不是非常便携(至少如果你对嵌入式设备感兴趣)。目前大多数Linux操作系统都使用PulseAudio作为他们的音频服务器,它还有一个简单的API,只用于播放单个声音,而且是一个非常难以用于低级别的API。然后有gstreamer库,它相当便携,有半路下降API,专门用于播放音频和视频文件和流,所以可能是继续使用linux的方法。

I personally recommend to use PortAudio, which is a library that provides – as the name states – an audio framework which is portable across several operating systems and audio frameworks. PortAudio is also well designed and has a simple but flexible API.

我个人建议使用PortAudio,它是一个提供 - 如名称所示 - 的音频框架,可以跨多个操作系统和音频框架移植。 PortAudio设计精良,具有简单但灵活的API。

#3


1  

Well, I'm not sure if current desktops have some more advanced services/libraries that play audio files, but if you really want to do some low-level OS audio output, start looking here. Maybe this is a little too low-level for your needs.

好吧,我不确定当前桌面是否有一些更高级的服务/库来播放音频文件,但如果你真的想做一些低级别的OS音频输出,请从这里开始。也许这对你的需求来说太低了。

#1


4  

You can just open("/dev/dsp") and write to it. That's as "native syscall" as you can get, I believe.

你可以打开(“/ dev / dsp”)并写入它。我相信,这就像你可以获得的“原生系统调用”一样。

#2


3  

Concerning Audio: Linux != Mac, so I will answer both separately.

关于音频:Linux!= Mac,所以我会单独回答。

MacOS uses the core audio framework for low level audio. Building on that there are several higher level APIs. If you just want to play a sound file the AV Foundation Framework is most likely the way to go. Here you find an example to build a simple app for iOS, which can be ported to MacOS easily.

MacOS将核心音频框架用于低级音频。在此基础上,有几个更高级别的API。如果您只想播放声音文件,AV Foundation Framework很可能是最佳选择。在这里,您可以找到为iOS构建简单应用程序的示例,该应用程序可以轻松移植到MacOS。

For Linux the whole situation is quite delicate. There exist many different frameworks and libraries and it very unclear which is low and which is high level, since some of them are emulating the others. Basically there is ALSA and OSS for low level audio. Both of them are crap. Jack is aiming to provide a framework for professional audio but it's not very portable (at least if you are interested in embedded devices). Most Linux OS currently are using PulseAudio as their audio server, which has also a simple API for just playing a single sound and a very difficult API for low level stuff. And then there is the gstreamer library, which is rather portable and has a half way descent API and is especially designed for playing audio & video files and streams, and so probably the way to go on linux.

对于Linux来说,整个情况非常微妙。存在许多不同的框架和库,并且非常不清楚哪个是低的并且是高级的,因为其中一些正在模仿其他的。基本上ALSA和OSS用于低级音频。他们俩都是垃圾。 Jack的目标是提供专业音频的框架,但它不是非常便携(至少如果你对嵌入式设备感兴趣)。目前大多数Linux操作系统都使用PulseAudio作为他们的音频服务器,它还有一个简单的API,只用于播放单个声音,而且是一个非常难以用于低级别的API。然后有gstreamer库,它相当便携,有半路下降API,专门用于播放音频和视频文件和流,所以可能是继续使用linux的方法。

I personally recommend to use PortAudio, which is a library that provides – as the name states – an audio framework which is portable across several operating systems and audio frameworks. PortAudio is also well designed and has a simple but flexible API.

我个人建议使用PortAudio,它是一个提供 - 如名称所示 - 的音频框架,可以跨多个操作系统和音频框架移植。 PortAudio设计精良,具有简单但灵活的API。

#3


1  

Well, I'm not sure if current desktops have some more advanced services/libraries that play audio files, but if you really want to do some low-level OS audio output, start looking here. Maybe this is a little too low-level for your needs.

好吧,我不确定当前桌面是否有一些更高级的服务/库来播放音频文件,但如果你真的想做一些低级别的OS音频输出,请从这里开始。也许这对你的需求来说太低了。