02-21-2024, 04:07 PM
(02-21-2024, 06:38 AM)1micha.elok Wrote:(02-20-2024, 06:52 PM)johnno56 Wrote: ... I am not expecting piano tones...
.. What I am expecting is a Synthesizer...
Just curious with "do re mi" generated by SquareWave function,
let's try this Pianoman .....
Code:'----------------
' INITIALIZATION
'----------------
include "sfx.n7"
set window "Pianoman", 160, 200,false,3
constant DO = 261.63*2
constant RE = 293.66*2
constant MI = 329.63*2
constant FA = 349.23*2
constant SO = 392.00*2
constant LA = 440.00*2
constant TI = 493.88*2
constant b1 = 0.2
constant b2 = 0.4
visible sfx = SFX() 'create an sfx instance
'--------------
' MAIN PROGRAM
'--------------
wln "PIANOMAN"
wln
wln "1 - Play DO"
wln "2 - Play RE"
wln "3 - Play MI"
wln "4 - Play FA"
wln "5 - Play SO"
wln "6 - Play LA"
wln "7 - Play TI"
wln "8 - Play Song"
wln
wln "Esc - Quit"
while not keydown(KEY_ESCAPE)
if keydown(KEY_1, true) then Piano(b1,DO)
if keydown(KEY_2, true) then Piano(b1,RE)
if keydown(KEY_3, true) then Piano(b1,MI)
if keydown(KEY_4, true) then Piano(b1,FA)
if keydown(KEY_5, true) then Piano(b1,SO)
if keydown(KEY_6, true) then Piano(b1,LA)
if keydown(KEY_7, true) then Piano(b1,TI)
if keydown(KEY_8, true) then Song()
wait 1
wend
'-----------
' FUNCTIONS
'-----------
function Piano(b,x)
'SquareWave(duration,freq,volume)
mytone = sfx.SquareWave(b,x,0.05)
play sound mytone
Pause()
endfunc
function Song()
Piano(b2,SO);Piano(b1,SO);Piano(b1,SO)
Piano(b2,SO);Piano(b2,MI)
Piano(b2,DO*2);Piano(b1,DO*2);Piano(b1,DO*2)
Piano(b2,TI);Piano(b2,LA)
Piano(b2,SO);Piano(b1,SO);Piano(b1,SO)
Piano(b2,LA);Piano(b1,SO);Piano(b1,SO)
Piano(b2,SO); Piano(b1,FA); Piano(b1,MI)
Piano(b2,RE); Piano(b1,DO); Piano(b1,RE)
Piano(b2,MI); Piano(b1,MI); Piano(b1,MI)
Piano(b2,RE); Piano(b2,RE)
Piano(b1,DO); Piano(b1,RE); Piano(b1,MI);Piano(b1,DO)
Piano(b1,LA/2);Piano(b1,TI/2);Piano(b2,DO)
Piano(b2,SO/2);Piano(b1,DO); Piano(b1,RE)
Piano(b2,SO); Piano(b1,FA); Piano(b1,MI)
Piano(b2,RE); Piano(b1,RE); Piano(b1,RE); Piano(b2*2,DO)
endfunc
function Pause()
wait 400
endfunc
Nice But you're creating new sound effects every time the user presses a key. Sound effect (and other resources, such as images) aren't garbage collected (they can't be). So the program will eat more and more memory. Better create the different piano sound effects once and re-use them, or release the old sound when a new one is created ('free sound').