Fysiikan kolmos kurssin kunniaksi tein iki ihanan puhallinsoitin simulaation. Koodi on rumaa, mutta pitäisi toimia.
Code: Select all
FrameLimit 10 // Rajoitellaan hitusen
Const SCALE = 100 // Mittakaava 100 pixeliä = 1m
speed As Float = 340 // Äänennopeus ilmassa
length As Float = 20 // Aloituspituus
ends As Integer = 0 // Suljettujen päiden lukumäärä
tune = 0 // Ylävärähtely
Repeat
Text 2, 2, "Pituus: " + length / SCALE + "m"
Text 396 - TextWidth("Taajuus: " + Frequency(length / SCALE, 340 , tune, ends) + "Hz"), 2, "Taajuus: " + Frequency(length / SCALE, 340, tune, ends) + "Hz"
Box 394 - TextWidth("Taajuus: " + Frequency(length / SCALE, 340 , tune, ends) + "Hz"), 1, 300, 16, 0
Text 2, 284, "Suljetut päät: " + ends
Text 400 - TextWidth("Ylävärähtely: " + tune), 284, "Ylävärähtely: " + tune
length = max(1, length + button(2, 20, 2) - button(2, 45, 3))
ends = Max(0, Min(1, ends - button(2, 262, 1) + button(24, 262, 0)))
tune = Max(0, tune - button(356, 262, 1) + button(378, 262, 0))
Line 200 - length / 2, 140, 200 + length / 2, 140
Line 200 - length / 2, 160, 200 + length / 2, 160
If ends = 1 Then Line 200 + length / 2, 140, 200 + length / 2, 160
If play = 0 Then
If txtbutton(200 - TextWidth("Soita") / 2, 180, "Soita") Then
play = 1
sound = SinWave(Frequency(length / SCALE, 340, tune, ends), 2)
playtimer = Timer()
PlaySound sound
EndIf
EndIf
If Timer() - playtimer > 2000 Then play = 0 Else txtbutton(200 - TextWidth("Soitetaan") / 2, 180, "Soitetaan")
DrawScreen
Forever
Function txtbutton(x,y,txt$)
w = TextWidth(txt) + 4
h = TextHeight(txt) + 4
Box x, y, w, h, 0
Text x + 2, y + 2, txt
If MouseHit(1) And MouseX() > x And MouseX() < x + w And MouseY() > y And MouseY() < y + h Then Return 1
EndFunction
Function Button(x,y,dir)
Box x, y, 20, 20, 0
Select dir
Case 0
Line x + 6, y + 6, x + 6, y + 14
Line x + 6, y + 6, x + 14, y + 10
Line x + 14, y + 10, x + 6, y + 14
Case 1
Line x + 14, y + 6, x + 14, y + 14
Line x + 6, y + 10, x + 14, y + 6
Line x + 14, y + 14, x + 6, y + 10
Case 2
Line x + 6, y + 14, x + 14, y + 14
Line x + 6, y + 14, x + 10, y + 6
Line x + 14, y + 14, x + 10, y + 6
Case 3
Line x + 6, y + 6, x + 14, y + 6
Line x + 6, y + 6, x + 10, y + 14
Line x + 10, y + 14, x + 14, y + 6
EndSelect
If MouseDown(1) And MouseX() > x And MouseX() < x + 20 And MouseY() > y And MouseY() < y + 20 Then Return 1
EndFunction
Function Frequency(length As Float, speed As Float, tune As Integer, ends As Integer)
lambda As Float = (length * (2 + 2 * ends)) / (tune + 1)
Return speed / lambda
EndFunction
Function SinWave(taajuus#, pituus#, voimakkuus#=100)
fq=44100
length=pituus*fq
f=OpenToWrite("tmpwav.tmp")
WriteInt f,$52494646
WriteInt f,0
WriteInt f,$57415645
WriteInt f,$666d7420
WriteInt f,16
WriteShort f,1
WriteShort f,1
WriteInt f,fq
WriteInt f,fq*2
WriteShort f,2
WriteShort f,16
WriteInt f,$64617461
WriteInt f,length*2
vokke = voimakkuus/100*32767
For i = 0 To length-1
WriteShort f,Sin(360.0*taajuus/fq*(i Mod fq))*vokke
Next i
fs=FileOffset(f)-8
SeekFile f,4
WriteInt f,fs
CloseFile f
tmpsnd=LoadSound("tmpwav.tmp")
DeleteFile "tmpwav.tmp"
Return tmpsnd
EndFunction