QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The _MEMSOUND function returns a _MEM value referring to a sound’s raw data in memory using a designated sound handle created by the _SNDOPEN function.

Syntax

imageBlock = _MEMSOUND[(soundHandle&, channel%)]

Parameter(s)

Description

Availability

Example(s)

Checking that a sound file is stereo.


song& = _SNDOPEN("song.wav") 'replace song.wav with a sound file you have
IF song& = 0 THEN PRINT "Load failed.": END

DIM leftchannel AS _MEM, rightchannel AS _MEM
leftchannel = _MEMSOUND(song&, 1)
rightchannel = _MEMSOUND(song&, 2)

IF rightchannel.SIZE > 0 THEN PRINT "This file is STEREO"
IF rightchannel.SIZE = 0 AND leftchannel.SIZE > 0 THEN
    PRINT "This file is MONO"
ELSEIF rightchannel.SIZE = 0 AND leftchannel.SIZE = 0 THEN
    PRINT "An error occurred."
END IF

_SNDCLOSE song& 'closing the sound releases the mem blocks

Plotting a sound’s waves.


SCREEN _NEWIMAGE(800, 327, 32)
song& = _SNDOPEN("drums.ogg") 'replace drums.ogg with a sound file you have
IF song& = 0 THEN PRINT "Load failed.": END

DIM leftchannel AS _MEM
leftchannel = _MEMSOUND(song&, 1)

IF leftchannel.SIZE = 0 THEN
    PRINT "An error occurred."
    END
END IF

DIM i AS _OFFSET
i = 0
DO
    _MEMGET leftchannel, leftchannel.OFFSET + i, a% 'get sound data
    LOCATE 1, 1: PRINT i; "/"; leftchannel.SIZE
    LINE (x, _HEIGHT / 2)-STEP(0, a% / 100), _RGB32(0, 111, 0) 'plot wave
    x = x + 1
    IF x > _WIDTH THEN
        x = 0
        LINE (0, 0)-(_WIDTH, _HEIGHT), _RGB32(0, 120), BF 'fade out screen
    END IF
    i = i + 2
    IF i + 2 > leftchannel.SIZE THEN EXIT DO
    _LIMIT 500
LOOP

_SNDCLOSE song& 'closing the sound releases the mem blocks

See Also