all platforms sound library
-
- Posts: 30
- Joined: Oct 28, 2023 13:31
all platforms sound library
Is there a sound library for FreeBasic working on all platforms (Windows, Linux, DOS, Emscripten)?
Just some beeps with different pitches would be enough, but loading some sound files would be better.
Just some beeps with different pitches would be enough, but loading some sound files would be better.
Re: all platforms sound library
https://sourceforge.net/projects/freeba ... ary/files/
Windows, Linux and DOS (had troubles getting it to work on Emscripten, sorry)
Windows, Linux and DOS (had troubles getting it to work on Emscripten, sorry)
-
- Posts: 30
- Joined: Oct 28, 2023 13:31
Re: all platforms sound library
What are the troubles with the Emscripten platform like?
I created some games using other Emscripten based exporters (Godot) and one common issue is that modern browsers do not allow audio output without the user has pressed a start button (in HTML/CSS/JS to start the Godot/FreeBasic program, not after the Godot/FreeBasic program has loaded).
I created some games using other Emscripten based exporters (Godot) and one common issue is that modern browsers do not allow audio output without the user has pressed a start button (in HTML/CSS/JS to start the Godot/FreeBasic program, not after the Godot/FreeBasic program has loaded).
Re: all platforms sound library
If I recall correctly, I had issues compiling under Emscripten a program that contains COMMON SHARED variables.
-
- Posts: 30
- Joined: Oct 28, 2023 13:31
Re: all platforms sound library
Can you share the not working code (or an example code showing the issue)? Maybe someone in this forum could help you.
Re: all platforms sound library
I haven't tried it in a while, but if I recall, any case of two different files compiled together using common shared had the issue
-
- Posts: 30
- Joined: Oct 28, 2023 13:31
Re: all platforms sound library
This sounds like an error in the FB compiler. Maybe you could file an issue at https://github.com/freebasic/fbc/issues ?
Re: all platforms sound library
I haven't experimented enough with it to narrow down the issue. Feel free to try compiling FB sfx with Emscripten and see what happens, by the way, I have told you all I recall about it.
Re: all platforms sound library
Well, after some experiments I figured out that it just doesn't create the variable in any module. So, by replacing it with "extern", and creating the variable by hand, it seems to work.linuxanddos wrote: ↑May 02, 2025 18:05 This sounds like an error in the FB compiler. Maybe you could file an issue at https://github.com/freebasic/fbc/issues ?
This is the driver for PCM audio in Emscripten:
Code: Select all
#include once "AL/al.bi"
#include once "AL/alc.bi"
extern __Samplerate as integer, __channels as integer, __bits_used as integer
dim __Samplerate as integer, __channels as integer, __bits_used as integer
dim shared as ALCdevice ptr ALDevice
dim shared as ALCcontext ptr ALContext
dim shared _format as integer
dim shared as ALuint _source
sub SoundSet(frequency as integer, channels as integer, bits as integer)
__Samplerate=frequency
__channels=channels
__bits_used=bits
if channels=2 then
if bits=8 then
_format=AL_FORMAT_STEREO8
else
_format=AL_FORMAT_STEREO16
end if
else
if bits=8 then
_format=AL_FORMAT_MONO8
else
_format=AL_FORMAT_MONO16
end if
end if
if ALContext=0 then
ALDevice=alcOpenDevice(0)
ALContext=alcCreateContext(ALDevice, 0)
alcMakeContextCurrent(ALContext)
alGenSources(1, @_source)
end if
end sub
sub playbuffer (soundBuffer as any ptr, buffersize as integer)
dim as ALuint buffer
dim as ALint processed
alGetSourcei(_source, AL_BUFFERS_PROCESSED, @processed)
while processed>0
processed-=1
alSourceUnqueueBuffers(_source, 1, @buffer)
alDeleteBuffers(1,@buffer)
wend
alGenBuffers(1, @buffer)
alBufferData(buffer, _format, soundBuffer, buffersize, __Samplerate)
alSourceQueueBuffers(_source, 1, @buffer)
dim as ALint state
alGetSourcei(_source, AL_SOURCE_STATE, @state)
if state <> AL_PLAYING then alSourcePlay(_source)
end sub
-
- Posts: 30
- Joined: Oct 28, 2023 13:31
Re: all platforms sound library
Wow, that's great!
If Midi is working someday: Could you update your project on SourceForge?
If Midi is working someday: Could you update your project on SourceForge?
Re: all platforms sound library
I plan to do that.
To implement MIDI, a possible solution would be to use Web MIDI API, but it has several issues: it works only on secure sites, it requires MIDI hardware to be present (I think), and it is not implemented in FireFox. So, instead, my plan is to build only a software synthesizer (most MIDI player for web pages do that, too)
To implement MIDI, a possible solution would be to use Web MIDI API, but it has several issues: it works only on secure sites, it requires MIDI hardware to be present (I think), and it is not implemented in FireFox. So, instead, my plan is to build only a software synthesizer (most MIDI player for web pages do that, too)
Re: all platforms sound library
Ok, done and published on sourceforge.linuxanddos wrote: ↑May 15, 2025 8:58 Wow, that's great!
If Midi is working someday: Could you update your project on SourceForge?
Added the Emscripten backend, fixed a bug in the midi backend for linux, and made a very little change in the sequencer (added a sleep command: it's needed for the emscripten version, but useful for the other versions as well, since it should reduce the pressure on the CPU)
Under Emscripten, MIDI should work both in foreground and background (thanks to timer callbacks). It uses a software implementation of FM synthesis, so quality is similar to the one achieved with the old SoundBlaster card. The main advantage is that no extra sound font file is needed, the synthesizer is very compact.
-
- Posts: 30
- Joined: Oct 28, 2023 13:31
Re: all platforms sound library
Thank you very much.
In the next time, I will do some tests and give feedback.
In the next time, I will do some tests and give feedback.