Page 1 of 1

[Windows x64] RSOUND - RetroSound library - DEV

Posted: Sat Aug 05, 2023 12:21 pm
by Mijikai
I am trying to write a small sound library to play/manipulate pcm sounds.
Maybe there is someone who wants to help or just wants to experiment with it in its current state :)
Let me know what you think.

Note:
Everything is WIP and may change in the future :)

Updated 08.09.2023

Version: DEV 0.03 (44100 @ 16 bit - 1 Channel)
Include:

Code: Select all

EnableExplicit

;--------------------------------------------------------------------------------------
; RSND - RetroSound @ 16 bit - mono
; Experimental (OLDSCHOOL) Sound Library 
; Written in FASM 1.37.30
;--------------------------------------------------------------------------------------
; Version: DEV 0.03
; Author: Mijikai
; Platform: Windows x64
;--------------------------------------------------------------------------------------
; Copyright 2023 by Mijikai all rights reserved
;--------------------------------------------------------------------------------------
; License:
; Attribution-NonCommercial-NoDerivatives 4.0 International:
; https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode
;--------------------------------------------------------------------------------------

#RSND_VERSION = $0003

Import "winmm.lib"
EndImport

Import "rsnd.lib"
  rsndOpen.i(*Render);<- calls a user defined callback for every speaker cone position
  rsndClose.i()
  rsndVersion.i()
EndImport
Download dev.0.03:
https://www.dropbox.com/scl/fi/1h4e6h0l ... r7jlm&dl=0

--------------------------------------------------------------------------------------

Version: DEV 0.02
Include:

Code: Select all

EnableExplicit

;--------------------------------------------------------------------------------------
; RSND - RetroSound @ 16 bit - mono
; Experimental (OLDSCHOOL) Sound Library 
; Written in PureBasic 6.02 (C-Backend)
;--------------------------------------------------------------------------------------
; Version: DEV 0.02
; Author: Mijikai
; Platform: Windows x64
;--------------------------------------------------------------------------------------
; Copyright 2023 by Mijikai all rights reserved
;--------------------------------------------------------------------------------------
; License:
; Attribution-NonCommercial-NoDerivatives 4.0 International:
; https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode
;--------------------------------------------------------------------------------------

#RSND_VERSION = $0002

Import "rsnd.lib"
  rsndOpen.i(*Render);<- calls a user defined callback for every speaker cone position
  rsndFormat.i();<- returns a pointer to a WAVEFORMATEX struct describing the format
  rsndClose.i()
  rsndVersion.i()
EndImport
Example:

Code: Select all

EnableExplicit

;--------------------------------------------------------------------------------------
; RSND - RetroSound @ 16 bit - mono
; Experimental (OLDSCHOOL) Sound Library 
; Written in PureBasic 6.02 (C-Backend)
;--------------------------------------------------------------------------------------
; Version: DEV 0.02
; Author: Mijikai
; Platform: Windows x64
;--------------------------------------------------------------------------------------

XIncludeFile "rsnd.pbi"

Procedure.i waveSine(Amplitude.f,Frequence.f);thx @idle :)
  Static phase.f
  phase + (6.2832 * Frequence) / 44100
  If phase > 6.2832
    phase - 6.2832
  EndIf
  ProcedureReturn 32767 * (Amplitude * Sin(phase))
EndProcedure

Procedure.i Render(*Cone.Word)
  *Cone\w = waveSine(0.1,440.0);<- generate a sine wave
  ProcedureReturn #Null
EndProcedure

Procedure.i Main()
  If OpenConsole("rsnd")
    If rsndOpen(@Render())
      Input()
      rsndClose()
    EndIf
    CloseConsole()
  EndIf
  ProcedureReturn #Null  
EndProcedure

End Main()
Download dev.0.02:
https://www.dropbox.com/scl/fi/syr79kzw ... aavei&dl=0

--------------------------------------------------------------------------------------

Version: DEV 0.01
Include:

Code: Select all

EnableExplicit

;--------------------------------------------------------------------------------------
; RSOUND - RetroSound 
; Experimental (OLDSCHOOL) Sound Library 
; Written in PureBasic 6.02 (C-Backend)
;--------------------------------------------------------------------------------------
; Version: DEV 0.01
; Author: Mijikai
; Platform: Windows x64
;--------------------------------------------------------------------------------------
; Copyright 2023 by Mijikai all rights reserved
;--------------------------------------------------------------------------------------
; License (MAY CHANGE IN THE FUTURE):
; Attribution-NonCommercial-NoDerivatives 4.0 International:
; https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode
;--------------------------------------------------------------------------------------

#RSND_VERSION = $0001

Enumeration RSND_FORMAT
  #RSND_FORMAT_PCM
  #RSND_FORMAT_FLT
EndEnumeration

Import "rsnd.lib"
  rsndOpen.i(Format.i,Channels.i,Rate.i,Bits.i)
  rsndFormat.i();<- returns a pointer to a WAVEFORMATEX struct
  rsndPlay.i(*Sound,Length.i,Copy.i = #False);<- *Sound is the sound buffer, Lenght its size - if Copy is #False the pointer to the buffer is used otherwise the buffer is copied
  rsndClose.i();<- cleanup everything
  rsndVersion.i()
EndImport
Example:

Code: Select all

EnableExplicit

XIncludeFile "rsnd.pbi"

Procedure.i Tone(Volume.i,Duration.i,FrequencyA.i,FrequencyB.i,NoiseA.i,NoiseB.i);<- Code by Joakim L. Christiansen / https://www.purebasic.fr/english/viewtopic.php?t=41844
  Protected *format.WAVEFORMATEX
  Protected *wave
  Protected sample.i
  Protected half.d
  Protected winc.d
  Protected finc.d
  Protected ninc.d
  Protected freq.d
  Protected nois.d
  Protected index.i
  Protected wdir.i
  Protected wavy.d
  Protected nlst.i
  Protected *note.Word
  *format = rsndFormat()
  Debug *format\nChannels
  sample = *format\wBitsPerSample >> 3
  Duration = ((*format\nSamplesPerSec * 0.001) * Duration) * sample
  *wave = AllocateMemory(Duration)
  If *wave
    Volume = (Volume * 0.001) * 32768
    half = *format\nSamplesPerSec / FrequencyA
    winc = Volume / half
    freq = FrequencyA
    nois = NoiseA + NoiseB
    If FrequencyB
      finc = (FrequencyB - FrequencyA) / (Duration * sample)
    EndIf
    If NoiseB
      ninc = (NoiseB - NoiseA) / (Duration * sample)
    EndIf
    *note = *wave
    For index = 0 To Duration Step 2
      If FrequencyB
        freq + finc
        half = *format\nSamplesPerSec / freq
        winc = Volume / half
      EndIf
      If NoiseB
        nois + ninc
      EndIf
      If wdir
        If wavy < Volume
          wavy + winc
          If wavy >= Volume
            wdir = 0  
          EndIf
        EndIf
      Else
        If wavy > - Volume
          wavy - winc
          If wavy <= - Volume
            wdir = 1  
          EndIf
        EndIf
      EndIf
      If nois
        If nlst >= nois
          wavy = Random(Volume)
          nlst = 0  
        Else
          nlst + 1
        EndIf
      EndIf
      *note\w = wavy
      *note + 2
    Next
    If rsndPlay(*wave,Duration,#True)
      ProcedureReturn #True
    EndIf
    FreeMemory(*wave)
  EndIf
  ProcedureReturn #False
EndProcedure

Procedure.i Main()
  If rsndOpen(#RSND_FORMAT_PCM,1,44100,16)
    Tone(80,500,500,1000,0,0)
    Tone(80,400,500,1000,0,0)
    Tone(80,100,1500,1000,0,0)
    Tone(50,2100,500,1000,0,0)
    Tone(80,400,1500,1000,0,0)
    OpenConsole(""):Input():CloseConsole()
    rsndClose()
  EndIf
  ProcedureReturn #Null
EndProcedure

End Main()
Download dev.0.01:
https://www.dropbox.com/scl/fi/2cos2wpb ... lqegg&dl=0

Re: [Windows x64] RSOUND - RetroSound library - DEV

Posted: Sat Aug 26, 2023 9:01 am
by thyphoon
Thanks for you share 🥰

Re: [Windows x64] RSOUND - RetroSound library - DEV

Posted: Mon Aug 28, 2023 8:40 pm
by Kwai chang caine
Works nice here :wink:
It remember to me the tons of hours spended on "Astro blaster" or "Space invader"

Image

There is a ..........

Image

"certain time" :mrgreen:


Thanks for sharing 8)

Re: [Windows x64] RSOUND - RetroSound library - DEV

Posted: Fri Sep 01, 2023 7:30 pm
by Mijikai
Thank you @thyphoon and @Kwai chang caine

Update

Version DEV 0.02:
- Now the sound can be manipulated in realtime, this could be useful for creating a little synth 8)

Re: [Windows x64] RSOUND - RetroSound library - DEV

Posted: Fri Sep 01, 2023 8:47 pm
by Kuron
Not tried this yet, but thank you. Much needed.

I have always wished for the "sound engine" from BBC Basic for Windows. Guess I could grab it out of the SDL version since that is open source, but dang it is so handy for retro games.

Re: [Windows x64] RSOUND - RetroSound library - DEV

Posted: Fri Sep 08, 2023 1:45 pm
by Mijikai
Thank you @Kuron
Kuron wrote: Fri Sep 01, 2023 8:47 pm I have always wished for the "sound engine" from BBC Basic for Windows...
It want to do something like that but im still not sure how and what exactly.
Some easy to use sound chip/engine thing.

Update

Version DEV 0.03:
- I have rewritten the lib in FASM now it is static and very small 8)
- I also fixed a small bug that was present in DEV 0.02

Re: [Windows x64] RSOUND - RetroSound library - DEV

Posted: Sat Sep 09, 2023 2:42 am
by Kuron
Mijikai wrote: Fri Sep 08, 2023 1:45 pm It want to do something like that but im still not sure how and what exactly.
Some easy to use sound chip/engine thing.
There is an open source version of BBC Basic for Windows called, BBC BASIC for SDL 2.0. It includes an improved version of the sound engine. The source can be found here. The author has encouraged the sound portion to be released on its own as a DLL, but that is beyond my skill level. Since it is targeted at all platforms, it could in theory be able to have a version of the sound engine for each version of PureBasic.

I am NOT promoting this language, I am merely mentioning it has an open source retro styled sound engine that could be easily made to work with PureBasic if somebody knows what they are doing.