Author Topic: how to playing audio files without delay ?  (Read 3756 times)

0 Members and 1 Guest are viewing this topic.

Offline mynameispaul

  • Newbie
  • Posts: 49
    • View Profile
how to playing audio files without delay ?
« on: February 07, 2019, 01:52:16 am »
is there a way to play an audio file without the delay? i'm guessing the initial delay is QB64 loading the entire audio file before playing it.

my goal is to make a program that acts like a musical keyboard when you press a key on the keyboard.
no matter how short the audio file or what format I use there is always a delay of about a second or two between pressing a key on the keyboard and then hearing the sound file being played.

can multiple audio files be loaded before being played?

thank you for any help in advance.

Offline ForSe

  • Newbie
  • Posts: 6
    • View Profile
Re: how to playing audio files without delay ?
« Reply #1 on: February 07, 2019, 03:21:11 am »

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Re: how to playing audio files without delay ?
« Reply #2 on: February 07, 2019, 05:29:43 am »
is there a way to play an audio file without the delay? i'm guessing the initial delay is QB64 loading the entire audio file before playing it.

my goal is to make a program that acts like a musical keyboard when you press a key on the keyboard.
no matter how short the audio file or what format I use there is always a delay of about a second or two between pressing a key on the keyboard and then hearing the sound file being played.

can multiple audio files be loaded before being played?

thank you for any help in advance.
I made a program, but it still isn't full proof. It is way too buggy for use. But, it ought to teach you how to use _SNDOPEN :D
P.S. Download the zip file if you want to actually run the program!!!
Code: QB64: [Select]
  1. 'Download the zip file if you want to actually run the program!!!
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. r = 100
  4. g = 200
  5. b = 255
  6. red& = _LOADIMAGE("music/red.png")
  7. blue& = _LOADIMAGE("music/blue.png")
  8. green& = _LOADIMAGE("music/green.png")
  9. DIM location$(12)
  10. DIM type$(12)
  11. DIM h&(12)
  12. DIM pic&(12)
  13. DIM x(12)
  14. DIM y(12)
  15. OPEN "music.txt" FOR INPUT AS #1
  16. FOR i = 1 TO 12
  17.     INPUT #1, location$(i)
  18.     INPUT #1, type$(i)
  19.     IF type$(i) = "pink" THEN pic&(i) = _LOADIMAGE("music/red.png")
  20.     IF type$(i) = "orange" THEN pic&(i) = _LOADIMAGE("music/green.png")
  21.     IF type$(i) = "loop" THEN pic&(i) = _LOADIMAGE("music/blue.png")
  22.     READ x(i), y(i)
  23.     here:
  24.     k$ = UCASE$(INKEY$)
  25.     FOR i = 1 TO 9
  26.         _PUTIMAGE (x(i), y(i))-(x(i) + 90, y(i) + 90), pic&(i)
  27.     NEXT i
  28.     maketiles
  29.  
  30.     IF LEN(k$) THEN
  31.         k = VAL(k$)
  32.         c = k
  33.         IF type$(k) = "loop" THEN
  34.             IF _SNDPLAYING(l&) = -1 THEN
  35.                 _SNDSTOP l&
  36.                 _SNDCLOSE l&
  37.             else
  38.             l& = _SNDOPEN(location$(k), "SYNC")
  39.             _SNDLOOP l&
  40.                         end if
  41.                 ELSEIF type$(k) = "orange" THEN
  42.             IF _SNDPLAYING(l&) = -1 THEN _SNDSTOP l&: _sndclose l&
  43.             IF _sndplaying(h&) = -1 THEN _SNDCLOSE h&
  44.             o& = _SNDOPEN(location$(k), "SYNC")
  45.             _SNDPLAY o&
  46.         ELSE
  47.             IF _SNDPLAYING(h&) = -1 THEN _sndstop h&: _SNDCLOSE h&
  48.             h& = _SNDOPEN(location$(k), "SYNC")
  49.             _SNDPLAY h&
  50.         END IF
  51.     END IF
  52.     LOCATE 1, 1: PRINT c
  53.     _DISPLAY
  54.     CLS
  55.  
  56. DATA 100,100,200,100,300,100,100,200,200,200,300,200,100,300,200,300,300,300,100,400,200,400,300,400
  57.  
  58. SUB maketiles ()
  59.     FOR i = 100 TO 300 STEP 100
  60.         FOR j = 100 TO 300 STEP 100
  61.             LINE (i, j)-(i + 90, j + 90), _RGB32(255, 255, 255), B
  62.         NEXT j
  63.     NEXT i
  64.  
  65. FUNCTION mouseclick (x1, y1, x2, y2)
  66.     mx = _MOUSEX
  67.     my = _MOUSEY
  68.     mb = _MOUSEBUTTON(1)
  69.  
  70.     IF mx >= x1 AND mx <= x2 AND my >= y1 AND my <= y2 AND mb THEN
  71.         mouseclick = -1
  72.     END IF
  73.  

SHOOT! Forgot to put music.txt here:
SAVE THIS AS MUSIC.TXT!!!
Code: QB64: [Select]
  1. music/1.wav
  2. pink
  3. music/2.wav
  4. pink
  5. music/3.wav
  6. pink
  7. music/4.wav
  8. music/5.wav
  9. music/6.wav
  10. music/7.wav
  11. orange
  12. music/8.wav
  13. orange
  14. music/9.wav
  15. orange
  16. music/A.wav
  17. pink
  18. music/B.wav
  19. pink
  20. music/C.wav
  21. pink
  22.  
-Prithak
« Last Edit: February 07, 2019, 06:00:21 am by Prithak »
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: how to playing audio files without delay ?
« Reply #3 on: February 07, 2019, 07:20:31 am »
Hi Paul. First step is loading all need sound files to memory. Let say, you use 30 sound files. Then write it so:

DIM sounds(30) AS LONG
sounds(1) = _SNDOPEN ("sound_1.mp3")
sounds(2) = _SNDOPEN ("sound_2.wav")
.
.
.
sounds(30) = _SNDOPEN ("sound_30.ogg")

this load this sound to memory, so it can be played without delay.
Next code is something as

DO
I$ = INKEY$
SELECT CASE I$
CASE "A", "a": _SNDPLAY (sounds(1))
.
.
.
CASE "Z", "z": _SNDPLAY (sounds(30))
END SELECT
LOOP


Erase all this sound files from memory before program end with one loop:

FOR kill_it = 1 to 30
_SNDCLOSE sounds(kill_it)
NEXT

SYSTEM

edit: so it would definitely be better to use _KEYHIT instead of INKEY$, because then you can play more than 1 sound when you press multiple keys at the same time.

« Last Edit: February 07, 2019, 07:27:39 am by Petr »

Offline mynameispaul

  • Newbie
  • Posts: 49
    • View Profile
Re: how to playing audio files without delay ?
« Reply #4 on: February 08, 2019, 11:38:30 pm »
Petr,
I tried your advice and unfortunately there is still a delay between pressing a button on the keyboard and the sound being  played. (maybe about a second or so).
Here is the code I'm using the test this:

--------------------------------------------------------------------------------------------------
DIM sounds(30) AS LONG

path$ = "...\Music\_new music\piano keys\main\short-128\"

tf$ = "~~wavs.tmp"
cmd$ = "dir " + CHR$(34) + path$ + "*.mp3" + CHR$(34) + " /b >" + tf$
SHELL cmd$

OPEN tf$ FOR INPUT AS #1

DO UNTIL EOF(1)

    LINE INPUT #1, x$
    i = i + 1
    sounds(i) = _SNDOPEN(path$ + x$, "LEN")

LOOP

CLOSE 1

total = i

DO

    k$ = INKEY$

    SELECT CASE k$
        CASE CHR$(27): EXIT DO

        CASE ELSE
            i = VAL(k$)
            IF i > 0 AND i <= total THEN
                _SNDPLAY sounds(i)
            END IF
    END SELECT

LOOP

CLS
SYSTEM
----------------------------------------------------------------------------------------

(I modified the path$ value for PII reasons to show here)

FellippeHeitor

  • Guest
Re: how to playing audio files without delay ?
« Reply #5 on: February 09, 2019, 12:05:23 am »
You are preloading the files correctly, so what's left to check is: isn't there a silent gap in the mp3 file? You'll want to open it with a wave editor and check it.

If after doing that you're certain the wave starts at the very beginning, then maybe converting to another format like ogg or wav may help.

Edit: second parameter to _SNDOPEN "LEN","VOL","SYNC", etc, is not required anymore as of version 1.2. They are all set by default anyway and the parameter is simply ignored.

Edit2: If you intend to play a loaded sound several times in a sequence (like repeating a note or a sound effect in a game), use _SNDPLAYCOPY instead of _SNDPLAY.
« Last Edit: February 09, 2019, 12:28:58 am by FellippeHeitor »