all platforms sound library

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Post Reply
linuxanddos
Posts: 30
Joined: Oct 28, 2023 13:31

all platforms sound library

Post by linuxanddos »

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.
angros47
Posts: 2407
Joined: Jun 21, 2005 19:04

Re: all platforms sound library

Post by angros47 »

https://sourceforge.net/projects/freeba ... ary/files/

Windows, Linux and DOS (had troubles getting it to work on Emscripten, sorry)
linuxanddos
Posts: 30
Joined: Oct 28, 2023 13:31

Re: all platforms sound library

Post by linuxanddos »

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).
angros47
Posts: 2407
Joined: Jun 21, 2005 19:04

Re: all platforms sound library

Post by angros47 »

If I recall correctly, I had issues compiling under Emscripten a program that contains COMMON SHARED variables.
linuxanddos
Posts: 30
Joined: Oct 28, 2023 13:31

Re: all platforms sound library

Post by linuxanddos »

Can you share the not working code (or an example code showing the issue)? Maybe someone in this forum could help you.
angros47
Posts: 2407
Joined: Jun 21, 2005 19:04

Re: all platforms sound library

Post by angros47 »

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
linuxanddos
Posts: 30
Joined: Oct 28, 2023 13:31

Re: all platforms sound library

Post by linuxanddos »

This sounds like an error in the FB compiler. Maybe you could file an issue at https://github.com/freebasic/fbc/issues ?
angros47
Posts: 2407
Joined: Jun 21, 2005 19:04

Re: all platforms sound library

Post by angros47 »

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.
angros47
Posts: 2407
Joined: Jun 21, 2005 19:04

Re: all platforms sound library

Post by angros47 »

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 ?
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.

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

I haven't made a driver for MIDI yet
linuxanddos
Posts: 30
Joined: Oct 28, 2023 13:31

Re: all platforms sound library

Post by linuxanddos »

Wow, that's great!
If Midi is working someday: Could you update your project on SourceForge?
angros47
Posts: 2407
Joined: Jun 21, 2005 19:04

Re: all platforms sound library

Post by angros47 »

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)
angros47
Posts: 2407
Joined: Jun 21, 2005 19:04

Re: all platforms sound library

Post by angros47 »

linuxanddos wrote: May 15, 2025 8:58 Wow, that's great!
If Midi is working someday: Could you update your project on SourceForge?
Ok, done and published 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.
linuxanddos
Posts: 30
Joined: Oct 28, 2023 13:31

Re: all platforms sound library

Post by linuxanddos »

Thank you very much.
In the next time, I will do some tests and give feedback.
Post Reply