DIR Return Create A Forum - Home
---------------------------------------------------------
techno game dev forum
HTML https://technogamedevforum.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: BASIC board
*****************************************************
#Post#: 110--------------------------------------------------
A Xylophone or Guitar from your keyboard
By: johnfree Date: May 20, 2012, 2:24 am
---------------------------------------------------------
In QB64 you can make very realistic sounds.
Decide which instruments you want and form an orchestra!
But to start simply here is a xylophone
You have decided (see code below) which keys (\,a,z,s.......;,/)
play which notes.
Yes (but not this demo) you can play chords and change between
wind, stringed, percussion and other instruments (timbre,
overtones, transients, decay rates)
Just get the hang of it here and come back with your ideas!
Download qb64 and run this program:
[code]
CONST Pi = 3.14159265358979
_DELAY 0.5
'time (in seconds)
'a$ = INKEY$: IF a$ = "" THEN GOTO nxt
nxt:
DO
nx: t = 0
a$ = INKEY$: IF a$ = "" THEN GOTO nx 'INPUT p
IF a$ = "\" THEN p = -9
IF a$ = "a" THEN p = -8 'b
IF a$ = "z" THEN p = -7
IF a$ = "s" THEN p = -6 'b
IF a$ = "x" THEN p = -5
IF a$ = "d" THEN p = -4.5
IF a$ = "c" THEN p = -4
IF a$ = "f" THEN p = -3 'b
IF a$ = "v" THEN p = -2
IF a$ = "g" THEN p = -1 'b
IF a$ = "b" THEN p = 0
IF a$ = "h" THEN p = 1 'b
IF a$ = "n" THEN p = 2
IF a$ = "j" THEN p = 2.5
IF a$ = "m" THEN p = 3
IF a$ = "k" THEN p = 4 'b
IF a$ = "," THEN p = 5
IF a$ = "l" THEN p = 6 'b
IF a$ = "." THEN p = 7
IF a$ = ";" THEN p = 8 'b
IF a$ = "/" THEN p = 9
a$ = ""
DO WHILE _SNDRAWLEN < 1
'this sets the overtones and their decay rates
F = EXP(-t) * COS(t * 2 * Pi * freq(p))
A = (1 / (4.5 * (t))) * COS(t * 2 * Pi * freq(p + 6 *
2.76))
c = (1 / (5 * (t))) * COS(t * 2 * Pi * freq(p + 6 *
5.4))
e = (1 / (6 * (t))) * COS(t * 2 * Pi * freq(p + 6 *
8.9))
value = 0.125 * (F + A + c + e) ' this volume-adds them
'Queue the value to be played
_SNDRAW value
t = t + 1 / _SNDRATE
LOOP
_LIMIT 10
LOOP 'WHILE t < 1
'Wait for sound buffer to empty
DO: LOOP WHILE _SNDRAWLEN
GOTO nxt
'Returns the frequency (in Hz) of the desired note
'measured in semitones from A440 (that's A above middle C)
'A above middle C is 0, A# is 1, B is 2, C is 3, etc.
'Middle C is -9
FUNCTION freq# (note AS DOUBLE)
freq = (2 ^ (note / 12)) * 440
END FUNCTION
[/code]
*****************************************************