SyntaxBomb - Indie Coders

Languages & Coding => BlitzMax / BlitzMax NG => MiniB3D => Topic started by: angros47 on October 24, 2019, 23:56:57

Title: Sound for OpenB3D using openAL
Post by: angros47 on October 24, 2019, 23:56:57
I saw several attempts to provide 3d sound to minib3d and OpenB3D. So, I made a port to C++:

https://sourceforge.net/projects/minib3d/files/OS3D%20pre-alpha.zip/download (https://sourceforge.net/projects/minib3d/files/OS3D%20pre-alpha.zip/download)

In this way, it could be used both in BlitzMax and in other languages. It can load .wav, .mp3, .flac and .ogg files, and it supports the "classic" features (3d sound, and Doppler effect), plus some new ones, like occlusion/obstruction/exclusion using OpenB3D geometry, and reverberation zones.
Title: Re: Sound for OpenB3D using openAL
Post by: Kippykip on October 27, 2019, 06:27:59
Awesome! I've been using FMOD in my OpenB3D project, I basically just followed the examples and made some types that handle distance from the camera, and added two pivots spaced left + right of the TCamera and do a bit of panning depending which pivot is closest to the sound entity. If it's blocked by a wall then it reduces the volume by 50%.
[yt_search]https://www.youtube.com/watch?v=lBbqYEsbG2s[/yt_search]

This is the module I'm using:
https://github.com/maxmods/bah.mod/tree/master/fmod.mod
Title: Re: Sound for OpenB3D using openAL
Post by: angros47 on October 27, 2019, 17:05:18
Interesting. My library allows to set any reduction of volume (not just 50%), and since it happens through a lowpass filter, low frequencies are less attenuated than high frequencies (as it happens in a real situation). To decide the volume of attenuated sound, you can use the last parameter of CreateListener (1: no attenuation, 0: sound is totally attenuated, behind an obstacle). To check if there is any obstacle, EntityVisible is used, so the obstacle must be a pickable entity.

Regarding the command CreateListener: the first three parameters (Entity, Rolloff factor, and Doppler scale) are supposed to work more or less like in Blitz3D. Differently from Blitz3D, there is no Distance Scale parameter, since such a parameter would make more sense if it is individually set for each entity: bigger entities would require a different scale factor than smaller ones; for that reason, distance scale is estimated according to the ScaleX factor of every entity that emits sounds.

Another difference is that, instead of having a SoundLoop parameter, the command EmitSound has an optional loop parameter: in this way, the same sound can be made looping for one entity, and not looping for another.

Commands like PauseChannel, ResumeChannel and StopChannel are not implemented, since they would just be aliases to alSourcePause, alSourcePlay and alSourceStop, so a simple macro would do the trick.

I am aware that FMOD is a very complete sound library, and it is the first choice for many engines: still, it is a proprietary solution, and its usage is not free for commercial applications. For that reason, I preferred to develop a free, and open source solution, based on open source resources (OpenAL)
Title: Re: Sound for OpenB3D using openAL
Post by: Kippykip on October 28, 2019, 08:54:24
Quote from: angros47 on October 27, 2019, 17:05:18
Interesting. My library allows to set any reduction of volume (not just 50%), and since it happens through a lowpass filter, low frequencies are less attenuated than high frequencies (as it happens in a real situation). To decide the volume of attenuated sound, you can use the last parameter of CreateListener (1: no attenuation, 0: sound is totally attenuated, behind an obstacle). To check if there is any obstacle, EntityVisible is used, so the obstacle must be a pickable entity.

Regarding the command CreateListener: the first three parameters (Entity, Rolloff factor, and Doppler scale) are supposed to work more or less like in Blitz3D. Differently from Blitz3D, there is no Distance Scale parameter, since such a parameter would make more sense if it is individually set for each entity: bigger entities would require a different scale factor than smaller ones; for that reason, distance scale is estimated according to the ScaleX factor of every entity that emits sounds.

Another difference is that, instead of having a SoundLoop parameter, the command EmitSound has an optional loop parameter: in this way, the same sound can be made looping for one entity, and not looping for another.

Commands like PauseChannel, ResumeChannel and StopChannel are not implemented, since they would just be aliases to alSourcePause, alSourcePlay and alSourceStop, so a simple macro would do the trick.

I am aware that FMOD is a very complete sound library, and it is the first choice for many engines: still, it is a proprietary solution, and its usage is not free for commercial applications. For that reason, I preferred to develop a free, and open source solution, based on open source resources (OpenAL)
Yeah I really need audio streaming and being able to measure DB's, which FMOD was the only one I could find with these implemented in BMX.
If OpenAL gets full support with the above then I'll be changing straight to that lmao
Title: Re: Sound for OpenB3D using openAL
Post by: angros47 on October 28, 2019, 17:42:12
What do mean with streaming? You want to be able to stream audio to other sources (like a chromecast)? Or to be able to play a stream (like an audio message coming from another user in real time?)

Also, the solution you used for panning is pretty clever, although it's not real 3d sound, since it would do nothing to simulate a sound coming from a source in front, or behind the listener, and it would do nothing to simulate the phase difference for each ear. Also, it could not take advantage form a system with more than two speakers. Your solution is stereo audio, not 3d audio (FMOD could provide 3d audio as well)
Title: Re: Sound for OpenB3D using openAL
Post by: Derron on October 28, 2019, 18:10:02
Streamed Audio for me means to eg play a longer ogg music file without having to load and completely decode it first.
So this also means one could play from a remote stream (some http(s)-ressource).


bye
Ron
Title: Re: Sound for OpenB3D using openAL
Post by: angros47 on October 28, 2019, 20:33:11
I see. But would you need that for 3d sound? In theory it could be done, in OpenAL, by splitting the audio stream into several buffers, and using alSourceQueueBuffers . So far, I never tried. Usually modern system have enough memory to load some minutes of uncompressed audio (also, a 3d audio source has to be mono, so it would take less memory)

Does FMOD offers streaming to sources for 3d audio?

Or all you need is a way to play a long stream of background music, with no 3d effect?
Title: Re: Sound for OpenB3D using openAL
Post by: Derron on October 28, 2019, 20:48:16
Dunno what kippykip requires - I just chimed in to mention what "streamed audio" could mean.

Regardless of "modern systems" or not: streamed ogg/mp3/... files also mean you could load them from the internet - while playing. Eg you could play back an internet radio stream.



bye
Ron
Title: Re: Sound for OpenB3D using openAL
Post by: angros47 on October 28, 2019, 21:12:32
I see. But it's not in the scope of my library. Also, any user who would like to listen an internet radio while playing could just open a web page (or a dedicated program) in background, there is no need to integrate such a function in the game itself
Title: Re: Sound for OpenB3D using openAL
Post by: markcwm on October 28, 2019, 21:17:00
Re: streamed audio, I was planning on using MaxMod2 by RedEye but the NG repo (https://github.com/bmx-ng/maxmod2.mod) seems to need a major update as it uses an older version that only does WAV + OGG and I couldn't get it to work. What you probably want is the GoogleCode archive (https://code.google.com/archive/p/maxmod/) v1.07 but it doesn't do MP3 so maybe that's out for you, it does do FLAC which is a great open-source alternative to MP3.
Title: Re: Sound for OpenB3D using openAL
Post by: Derron on October 28, 2019, 21:36:32
@ Internet radio
It was just an example of what streamed audio could mean - no mean to take this too serious. Maybe people want to stream their audio from a webservice (some live stream in game).
Also loading music files means
- load one by one with a delay on loading
- loading all in and use a lot of memory (assume 30 music tracks with 4 minutes = 120 minutes of uncompressed data = 900 MB audio or so)

In my game TVTower I allow players to extend audio playlists (so it chooses random tracks for a certain "area/screen" in the game). Could mean a lot of files to get loaded and decoded during startup/initialization.



@ markcwm
Yes, Brucey only ported what was needed (by me ;)). I am using "maxmod2" with NG + ogg on Windows (for Linux my streamed audio based on the Blitzmax-freeaudio seems to work without "cracklings").
Feel free to raise issues on github or a new thread here - as the current version compiles here and is in use (it played audio this morning :)).


bye
Ron
Title: Re: Sound for OpenB3D using openAL
Post by: angros47 on October 28, 2019, 21:44:56
Just to make the scope of this library clear: in original Blitz3D, there are some commands related to sound that are used also in BlitzBasic, and some commands that are related to 3d sounds, that are prerogative of Blitz3D.

Just as OpenB3D implements the 3d graphic commands, but not the 2d ones (since there are already other modules for that), this library implements only the commands related to 3d sound. Commands like PlayCDTrack or similar stuff are not planned.
Title: Re: Sound for OpenB3D using openAL
Post by: Kippykip on October 31, 2019, 05:03:21
Quote from: angros47 on October 28, 2019, 20:33:11
I see. But would you need that for 3d sound? In theory it could be done, in OpenAL, by splitting the audio stream into several buffers, and using alSourceQueueBuffers . So far, I never tried. Usually modern system have enough memory to load some minutes of uncompressed audio (also, a 3d audio source has to be mono, so it would take less memory)

Does FMOD offers streaming to sources for 3d audio?

Or all you need is a way to play a long stream of background music, with no 3d effect?
I use it for dynamic music in my game that has a big playlist for Idle/Action music (kind of like Oblivion).
Not having realtime streaming causes a big stutter and a lot of ram usage whenever a track changes otherwise.
For the actual 3D sounds I just load the whole thing, which I've also noticed that loading say .OGG files uses far less ram usage than the native FreeAudio driver BMX uses.
Title: Re: Sound for OpenB3D using openAL
Post by: angros47 on October 31, 2019, 22:10:10
Dynamic music, in my opinion, worked well with Midi music, or with FM synthesized music (like the original SCUMM engine did, for example... or also Wing Commander). In Wing commander, the music after a mission was always the same, but it was played in sad, or triumphant way, depending on how successful mission was. It was achieved by changing instruments, speed, and so on, while keeping the same notes
Title: Re: Sound for OpenB3D using openAL
Post by: Kippykip on November 07, 2019, 13:56:14
Quote from: angros47 on October 31, 2019, 22:10:10
Dynamic music, in my opinion, worked well with Midi music, or with FM synthesized music (like the original SCUMM engine did, for example... or also Wing Commander). In Wing commander, the music after a mission was always the same, but it was played in sad, or triumphant way, depending on how successful mission was. It was achieved by changing instruments, speed, and so on, while keeping the same notes
Yeah I really liked how Star Wars: Dark Forces did it for MS-Dos, depending on the action the midi would really smoothly transition into this battle/intense music.
Title: Re: Sound for OpenB3D using openAL
Post by: Derron on November 07, 2019, 14:17:32
NG might get its soloud.mod updated - it has 3d positional audio, audio streams, ... maybe it can replace freeaudio.mod later on


bye
Ron
Title: Re: Sound for OpenB3D using openAL
Post by: angros47 on November 07, 2019, 18:23:47
Here are some examples of how openAL allows to implement streaming:

https://developer.tizen.org/dev-guide/2.4/org.tizen.tutorials/html/native/multimedia/openal_tutorial_n.htm (https://developer.tizen.org/dev-guide/2.4/org.tizen.tutorials/html/native/multimedia/openal_tutorial_n.htm)
Title: Re: Sound for OpenB3D using openAL
Post by: Derron on November 07, 2019, 21:03:51
OpenAL is not default installed on all linux desktops - maybe it even cannot get installed.
(Soft)-OpenAL requires a .dll to ship with your binary - not everyone likes that. Exception is if your application is LGPL(compatible) too - means source available etc.

Not everyone wants to use that then. For our purposes (NG-platforms, 3D audio, streaming) SoLoud sounds promising - and more lightweight than OpenAL. Aside of thaet: At the end people use what is offered and "works".


bye
Ron
Title: Re: Sound for OpenB3D using openAL
Post by: angros47 on November 09, 2019, 23:59:36
OpenAL provides some effects that are useful for reverberation, I can't see anything equivalent in soLoud.

Also, how is soLoud 3D audio compared to openAL? A complete 3d sound should implement not just appropriate panning between left and right channel, but also phase difference, and filtering to simulate the distortion of outer ear, that allows to tell if the sound source is in front, or behind the listener. This is called Head Related Transfer Function (HTRF), and it's used in realistic binaural sound. Also, a 3d sound system should be able to support more than two channels, if available (for Dolby surround, for example).

According to documentation, soLoud for the 3d sound is able to support multiple channels, but cannot do HTRF, nor surround:
https://sol.gfxile.net/soloud/soloud_20150512.html#can-soloud-do-hrtf (https://sol.gfxile.net/soloud/soloud_20150512.html#can-soloud-do-hrtf)

True 3D sound should be an abstraction: one would set listener and source 3d coordinates, and the system would produce the most realistic sound using the available output: binaural if on headphones, surround if more than two speakers are available, and so on. OpenAL supports stereo, binaural and surround sound, soLoud supports only stereo at the moment.

(although the most recent manual states that surround is now supported)