_SNDNEW
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
The _SNDNEW function creates a raw empty sound in memory and returns a LONG handle value for later access.
Syntax
- soundHandle& = _SNDNEW(frames&[, channels&][, bits&][, sampleRate&])
Parameters
- frames& is the number of sample frames needed. The number needed for one second of sound is determined by your sound hardware's sample rate, hence you may use the following formula:
- frames& = _SNDRATE * neededSeconds! where you may also specify fractional seconds.
- channels& (optional) specifies the number of channels (1 = mono, 2 = stereo). Defaults to 1 (mono) if omitted.
- bits& (optional) specifies the number of bits per sample (8 = 8-bit unsigned integer, 16 = 16-bit signed integer, 32 = 32-bit floating point). Defaults to 32-bit if omitted.
- sampleRate& (optional) specifies the playback sample rate in Hz. Defaults to the system's default sample rate if omitted.
Description
- The _SNDNEW function creates an empty sound buffer in memory that can be filled with sample data.
- Once created, the sound buffer can be accessed and modified using the _MEM interface, particularly with _MEMSOUND, _MEMGET, and _MEMPUT.
- This function allows generating and storing sounds programmatically for repeated playback.
- The sound memory can be populated with sample data from various sources, such as files, DATA statements, or procedural generation.
- Sound memory pointers obtained with _MEMSOUND must be freed using _MEMFREE, and the sound handle itself must be released with _SNDCLOSE when no longer needed.
Availability
- Sample rate support was added in QB64-PE v4.2.0.
Examples
- Example 1
- Creating a sound at runtime and playing it.
_DEFINE A-Z AS LONG OPTION _EXPLICIT RANDOMIZE TIMER CONST SOUND_DURATION = 5 ' duration in seconds CONST SAMPLE_CHANNELS = 1 ' number of channes. For stereo, we need to add another _MEMPUT below and +offset by SAMPLE_BYTES CONST SAMPLE_BYTES = _SIZE_OF_SINGLE ' number of bytes/sample (not frame!) DIM h AS LONG: h = _SNDNEW(SOUND_DURATION * _SNDRATE, SAMPLE_CHANNELS, SAMPLE_BYTES * 8) IF (h < 1) THEN PRINT "Failed to create sound!" END END IF DIM sndblk AS _MEM: sndblk = _MEMSOUND(h, 0) IF sndblk.SIZE = 0 THEN _SNDCLOSE h PRINT "Failed to access sound data!" END END IF DIM t AS _INTEGER64 FOR t = 0 TO (SOUND_DURATION * _SNDRATE) - 1 _MEMPUT sndblk, sndblk.OFFSET + (t * SAMPLE_BYTES * SAMPLE_CHANNELS), (SIN(_PI(880! * t) / _SNDRATE) + (RND - RND)) / 2! AS SINGLE ' mixes noise and a sine wave at half volume NEXT _SNDPLAY h SLEEP SOUND_DURATION _SNDCLOSE h END |
- Example 2
- Creating sounds with different sample rates.
_DEFINE A-Z AS LONG OPTION _EXPLICIT RANDOMIZE TIMER CONST SOUND_DURATION = 3 ' duration in seconds CONST SAMPLE_CHANNELS = 1 ' mono CONST SAMPLE_BYTES = _SIZE_OF_SINGLE ' 32-bit floating point (4 bytes per sample) DIM h1 AS LONG, h2 AS LONG h1 = _SNDNEW(SOUND_DURATION * _SNDRATE, SAMPLE_CHANNELS, SAMPLE_BYTES * 8, _SNDRATE \ 2) h2 = _SNDNEW(SOUND_DURATION * _SNDRATE, SAMPLE_CHANNELS, SAMPLE_BYTES * 8, _SNDRATE) IF (h1 < 1) _ORELSE (h2 < 1) THEN PRINT "Failed to create sound!" END END IF DIM sndblk1 AS _MEM, sndblk2 AS _MEM sndblk1 = _MEMSOUND(h1, 0) sndblk2 = _MEMSOUND(h2, 0) IF (sndblk1.SIZE = 0) _ORELSE (sndblk2.SIZE = 0) THEN PRINT "Failed to access sound data!" _SNDCLOSE h1 _SNDCLOSE h2 END END IF DIM t AS _INTEGER64 FOR t = 0 TO (SOUND_DURATION * _SNDRATE) - 1 _MEMPUT sndblk1, sndblk1.OFFSET + (t * SAMPLE_BYTES), SIN(_PI(880! * t) / _SNDRATE) AS SINGLE NEXT FOR t = 0 TO (SOUND_DURATION * _SNDRATE) - 1 _MEMPUT sndblk2, sndblk2.OFFSET + (t * SAMPLE_BYTES), SIN(_PI(880! * t) / _SNDRATE) AS SINGLE NEXT PRINT "Playing sound at"; _SNDRATE \ 2; "Hz..." _SNDPLAY h1 SLEEP SOUND_DURATION * 2 PRINT "Playing sound at"; _SNDRATE; "Hz..." _SNDPLAY h2 SLEEP SOUND_DURATION _SNDCLOSE h1 _SNDCLOSE h2 END |
See also
- _MEM, _MEMSOUND, _MEMFREE
- _MEMPUT, _MEMGET, _MEMGET (function)
- _SNDOPEN, _SNDCLOSE, _SNDRAW, _SNDRATE