QB64 Phoenix Edition
incorporate sound files in code - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://staging.qb64phoenix.com/forumdisplay.php?fid=10)
+---- Thread: incorporate sound files in code (/showthread.php?tid=1343)



incorporate sound files in code - mdijkens - 12-29-2022

I always like to distribute my final program as just one single exe.
Therefore I include (small)images in DATA statements most of the time, by reading them in a Long array and _PutImage that array:
Code: (Select All)
Function data2image&
  Read iWidth%, iHeight%
  Dim alpha As _Byte, cval As Long, imgArray(iWidth% * iHeight%) As Long: imgArray(0) = iHeight% * 2 ^ 16 + iWidth%
  Read lin$: i64$ = String$(6 * iWidth% * iHeight%, 0): i64pos~& = 1
  Do While lin$ <> "*"
    l% = Len(lin$): Mid$(i64$, i64pos~&, l%) = lin$: i64pos~& = i64pos~& + l%: Read lin$
  Loop
  i$ = _Inflate$(base64decode$(Left$(i64$, i64pos~& - 1))): cn& = -3
  Do While n& < iWidth% * iHeight%
    cn& = cn& + 4: cval = CVL(Mid$(i$, cn&, 4)): alpha = _Alpha32(cval)
    If alpha = &H10 Then
      it& = cval - &H10000000: cn& = cn& + 4: cval = CVL(Mid$(i$, cn&, 4))
      Do While it& > 0
        n& = n& + 1: imgArray(n&) = cval: it& = it& - 1
      Loop
    Else
      n& = n& + 1: imgArray(n&) = cval
    End If
  Loop
  hImg& = _NewImage(iWidth%, iHeight%, 32): _Dest hImg&: Put (0, 0), imgArray(): _Dest 0: data2image& = hImg&
End Function


Is something like that also possible for ogg/wav/mp3 files?
Storing them in code in DATA statements, reading them in the correct (_MEM) structure and provide a Long pointer to that?


RE: incorporate sound files in code - mnrvovrfc - 12-29-2022

Yes but it's tedious. For much longer than a minute and decent quality, even MP3 and OGG start to become too big to involve directly into an EXE file, which would have a high chance of becoming corrupt, refusing to run and/or being mistaken for a virus.

Sadly, using "_INFLATE$" and "_DEFLATE$" wouldn't save a lot of space from files already compressed, although it does make it easier to package audio files into a compiled program.

https://qb64phoenix.com/qb64wiki/index.php/INFLATE$

https://qb64phoenix.com/qb64wiki/index.php/DEFLATE$


RE: incorporate sound files in code - mdijkens - 12-29-2022

Thanks.
Any suggestion how to do it?
Most sounds are less then 1 second (error, abort, warning, finish, etc.)


RE: incorporate sound files in code - mnrvovrfc - 12-29-2022

https://staging.qb64phoenix.com/showthread.php?tid=217

It could be easily adapted to work with sound files. Include the BASIC code that it creates into your program.

There is one drawback, however. At the moment QB64PE doesn't have a counterpart to "_NEWIMAGE" that is related to sound. It is one of the biggest requests.

https://github.com/QB64-Phoenix-Edition/QB64pe/issues/232

https://github.com/QB64-Phoenix-Edition/QB64pe/issues/166

What could be done at the moment is a suggestion, a messy one that I haven't tried. A string could be created which is the result of "_INFLATE$". Create a "_MEM" variable to point to the beginning of that string. Then an "empty" buffer which is as big (in sample frames) as the biggest sound file to be included has to be loaded in with "_SNDOPEN". Use "_MEMSOUND" to set the destination "_MEM" variable, then do a memory copy, keeping note of the length of the file that was loaded into the string. It might be playable with "_SNDPLAY", or in the least, could be worked on with "_SNDRAW".

It's slow and messy. It wouldn't work if the files are different sampling rates and/or bit rates. It's better to wait and see if there's a QB64PE v3.5 that could give us a "_SNDCREATEBUFFER" or something like that.

I have done a routine only in 44100Hz 16-bit mono wave files which is rather easy to program, loaded from a string and could be played back with "_SNDRAW". But the sound file support in QB64PE supports theoretically any sampling rate and bit rate, and stereo sound in addition.


RE: incorporate sound files in code - Dav - 12-29-2022

At the really old, old forum (qb64.net), I suggested adding RESOURCE feature to QB64 once, allowing resource files to be included and used in a program.  Galleon was open to the idea I recall.  I still would love to see that implemented one day.

The closest I've come to sticking small sound files in the EXE is using BASFILE, and when under windows the data doesn't have to extracted to disk to be played, it can be played from memory using Windows API.  Here is a collection of small sound effects using that method - I don't *think* I ever posted it here before.

- Dav


.bas   sfxcodes.bas (Size: 125.19 KB / Downloads: 67)


RE: incorporate sound files in code - mdijkens - 12-29-2022

Thanks, I see the issue now.
I don't like the overly complicated and tricky approach with a buffer (that still needs to be red with _SNDOPEN).
My solution needs to work under Windows & Linux

So, I'll leave it for now and (together with you) hope for a RESOURCE functionality someday :-)


RE: incorporate sound files in code - a740g - 01-02-2023

Happy new year!

This is coming to PE soonish...  Big Grin 
Well, it is already in for review. Miniaudio fixes by a740g · Pull Request #279 · QB64-Phoenix-Edition/QB64pe (github.com)