C++ 用于游戏的跨平台声音 API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3636715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:20:49  来源:igfitidea点击:

Cross platform sound API for games?

c++capiaudio

提问by jmasterx

Is there an API only for sound?

是否有仅用于声音的 API?

APIs such as Allegroor SDLprovide too much for my needs. I simply need a library that can do something like:

AllegroSDL等 API为我提供了太多的需求。我只需要一个可以执行以下操作的库:

InitSound();
Sound *door = LoadSound("door.wav");
PlaySound(door,volume);

It would also be great if it could support compressed formats such as Vorbisor MP3.

如果它可以支持VorbisMP3等压缩格式,那就太好了。

采纳答案by rochal

I'm a big fan of the SFML library. It does provide additional graphics and network features, but what is relevant to this question, is that it also has neat audio package.

我是 SFML 库的忠实粉丝。它确实提供了额外的图形和网络功能,但与这个问题相关的是它还有简洁的音频包。

Audio features are:

音频功能是:

  • Uses hardware acceleration whenever possible
  • Can load and save standard sound formats: Ogg, WAV, FLAC, AIFF, Au, RAW, paf, 8SVX, NIST, VOC, IRCAM, W64, MAT4, MAT5 PVF, HTK, SDS, AVR, SD2, Core Audio Format, WVE, MPC2K, RF64
  • Can load all audio resources directly from files in memory
  • 3D sound spacialization
  • Easy interface for capturing audio
  • Manages memory efficiently, so that you don't have to worry about resources lifetime or storage
  • Supports streaming for big files; you can even write your custom streaming class for any source (network, ...)
  • Supports multi-channels formats (mono, stereo, 4.0, 5.1, 6.1, 7.1)
  • 尽可能使用硬件加速
  • 可以加载和保存标准声音格式: OggWAVFLACAIFFAuRAW、paf、8SVX、NIST、VOC、IRCAMW64MAT4、MAT5 PVFHTKSDS、AVR、SD2Core Audio Format、WVE , MPC2K, RF64
  • 可以直接从内存中的文件加载所有音频资源
  • 3D声音空间化
  • 用于捕获音频的简单界面
  • 高效管理内存,让您不必担心资源寿命或存储
  • 支持大文件串流;您甚至可以为任何来源(网络,...)编写自定义流媒体类
  • 支持多声道格式(单声道、立体声、4.0、5.1、6.1、7.1)

Website: http://www.sfml-dev.org/

网站:http: //www.sfml-dev.org/

回答by Baget

Use PortAudio. It is a portable cross-platform audio API.

使用PortAudio。它是一个可移植的跨平台音频 API。

回答by graham.reeds

If you are use to using OpenGL then you might like to try OpenAL?

如果您习惯使用 OpenGL,那么您可能想尝试OpenAL吗?

The API is very similar to OpenGL so you should feel at home.

API 与 OpenGL 非常相似,因此您应该有宾至如归的感觉。

回答by Dragontamer5788

I really like SFML, it's written in C++ and follows most of the rules of good API design (RAIIand so forth).

我真的很喜欢SFML,它是用 C++ 编写的,并且遵循了大部分好的 API 设计规则(RAII等)。

If you prefer a more "C-like" library, SDLneeds to be mentioned.

如果您更喜欢更“类 C”的库,则需要提及SDL

回答by Bob

SDL_soundis very simple to use, and handles the decoding for you. If you used OpenAL, you would have to do the decoding yourself (last I used it, anyway). I actually prefer OpenAL for games, though.

SDL_sound使用起来非常简单,并为您处理解码。如果您使用OpenAL,则必须自己进行解码(无论如何,我最后一次使用它)。不过,我实际上更喜欢 OpenAL 的游戏。

回答by Simon Bosley

I know it's an old question but anyone stumbling upon this now might like to check out FMOD

我知道这是一个老问题,但现在任何遇到这个问题的人都可能想查看FMOD

You can make use of the low-level API to start simple then make use of the more powerful features (and FMOD Studio) later-on if required.

如果需要,您可以使用低级 API 开始简单,然后使用更强大的功能(和 FMOD Studio)。

Here's the documentationfor playing a sound, which references the samples in the API:

以下是播放声音的文档,其中引用了 API 中的示例:

This example shows how to simply load and play multiple sounds, the simplest usage of FMOD.

这个例子展示了如何简单地加载和播放多个声音,这是 FMOD 的最简单用法。

I used this low-level API for a cross platform (Mac, Windows) Space Invaders clone I developed and found it very useful!

我将这个低级 API 用于我开发的跨平台(Mac、Windows)Space Invaders 克隆,发现它非常有用!

回答by decasteljau

If you are looking for a professional cross-platform sound design API and authoring application, take a look at Audiokinetic Wwise. It can do much more than just play sounds. It has effects, complex sound logic system, sound layering, profiling, interactive music tools, etc... It provides Ogg Vorbisand MP3support, and it is free for non-commercial projects.

如果您正在寻找专业的跨平台声音设计 API 和创作应用程序,请查看Audiokinetic Wwise。它可以做的不仅仅是播放声音。它具有效果器、复杂的声音逻辑系统、声音分层、分析、交互式音乐工具等……它提供Ogg VorbisMP3支持,并且对于非商业项目是免费的。

回答by perarne

You may want to have a look at the Kowalski project, a cross platform audio API geared towards games and similar interactive applications. It's released under the zlib license.

您可能想查看Kowalski 项目,这是一个面向游戏和类似交互式应用程序的跨平台音频 API。它是在 zlib 许可下发布的。

回答by Izzy

Although it is only free for non-commercial applications, Bassis really easy to implement.

虽然它只对非商业应用免费,但Bass真的很容易实现。

Example of usage:

用法示例:

// initialize device at 44kHz
if (!BASS_Init(-1,44100,0,0,NULL))
{
    printf("Can't initialize device");
    return 0;
}

HSAMPLE sample;
HCHANNEL channel;

// load from disk
if( sample = BASS_SampleLoad(FALSE,"foo.wav",0,0,1,BASS_SAMPLE_OVER_POS) ) {

    // get playable channel
    channel = BASS_SampleGetChannel( sample,FALSE);

    // play
    BASS_ChannelPlay(channel,TRUE);

    // wait for playback to finish
    while( BASS_ChannelIsActive( channel ) )
         Sleep(100);

   BASS_SampleFree(sample);
}

// cleanup
BASS_Free();

回答by andrewrk

Consider libsoundio. It does not support compression or codecs, but it does provide a robust cross platform sound API. It might be a bit low level for your needs; the library you're looking for might be built on top of libsoundio.

考虑libsoundio。它不支持压缩或编解码器,但它提供了强大的跨平台声音 API。对于您的需求,它可能有点低;您正在寻找的库可能建立在 libsoundio 之上。