Code: Select all
#1 255
#2 0
#3 255
#4 0
jne...
Code: Select all
for i=1 to 100
piste((i mod 2)*255) // saha-aalto
next i
Code: Select all
#1 255
#2 0
#3 255
#4 0
jne...
Code: Select all
for i=1 to 100
piste((i mod 2)*255) // saha-aalto
next i
Code: Select all
//Code by Latexi95
SCREEN 800,400
wav = SM_OpenWav("Media/Blaster.wav") //Avataan wav
rawdata = SM_GetChannelRawData(wav)//Haetaan data
length = MEMBlockSize(rawdata) //Muistipalan koko
keskiarvo = i2 = i3 = 0
For i = 0 To length - 4 Step 4
PokeFloat rawdata,i,PeekInt(rawdata,i)/2147483647.0 //Muutetaan arvot sopivammiksi
Next i
x = MouseMoveX() : y = MouseMoveY() //Nollataan
x = 0
y = 200
Zoom# = 1.0
Repeat
lasty = y
Color cbWhite
Line 0,y+Zoom#*200.0,800,y + Zoom#*200.0 //Maksimi
Line 0,y-Zoom#*200.0,800,y - Zoom#*200.0
Color cbRed
For i = 0 To length - 4 Step 4
y2 = y+PeekFloat(rawdata,i)*Zoom#*200.0
Line x+(i-4)*Zoom#,lasty,x+i*Zoom#,y2
lasty = y2
Next i
mx = MouseMoveX()
my = MouseMoveY()
If MouseDown(1) Then x = x + mx : y = y + my
Zoom# = Max(Zoom# + Zoom# * MouseMoveZ()/10,0.1)
DrawScreen
Forever
//Wav reader by Latexi95
Function SM_OpenWav(path$)
If FileExists(path$) = False Then MakeError "SM_OpenWav failed! File: "+path$+" not found."
f = OpenToRead(path$)
If Not(ReadByte(f) = $52 And ReadByte(f) = $49 And ReadByte(f) = $46 And ReadByte(f) = $46) Then MakeError "Wav-file is not valid."
chunksize = ReadInt(f)
If not(ReadByte(f) = $57 And ReadByte(f) = $41 And ReadByte(f) = $56 And ReadByte(f) = $45) Then MakeError "Wav-file is not valid."
If Not(ReadByte(f) = $66 And ReadByte(f) = $6d And ReadByte(f) = $74 And ReadByte(f) = $20) Then MakeError "Wav-file is Not valid."
chunk1size = ReadInt(f)
audioformat = ReadShort(f)
numofchannels = ReadShort(f)
samplerate = ReadInt(f)
byterate = ReadInt(f)
blockalign = ReadShort(f)
bitspersample = ReadShort(f)
If Not(ReadByte(f) = $64 And ReadByte(f) = $61 And ReadByte(f) = $74 And ReadByte(f) = $61) Then MakeError "Wav-file is Not valid."
datasize = ReadInt(f)
datalen = datasize/numofchannels/(bitspersample/8)
mem = MakeMEMBlock(16+datalen*numofchannels*4)
PokeByte mem,0,numofchannels
PokeInt mem,1,samplerate
PokeInt mem,5,byterate
PokeShort mem,9,blockalign
PokeByte mem,11,bitspersample
PokeInt mem,12,datalen
index = 16
Select bitspersample
Case 16
For i2 = 1 To numofchannels
For i = 1 To datalen
value = ReadShort(f) Shl 16
PokeInt mem,index,value
index + 4
Next i
Next i2
Case 32
For i2 = 1 To numofchannels
For i = 1 To datalen
value = ReadInt(f)
PokeInt mem,index,value
index + 4
Next i
Next i2
Default
For i2 = 1 To numofchannels
For i = 1 To datalen
value = (Int(ReadByte(f))-128)*16777216
PokeInt mem,index,value
index + 4
Next i
Next i2
EndSelect
CloseFile f
Return mem
EndFunction
Function SM_MakeWav(channels = 1,samplerate = 16000)
wav = MakeMEMBlock(16+channels)
PokeByte wav,0,channels
PokeInt wav,1,samplerate
PokeInt wav,12,1
Return wav
EndFunction
Function SM_GetSampleRate(wav)
Return PeekInt(wav,1)
EndFunction
Function SM_SetSampleRate(wav,rate)
PokeInt wav,1,rate
EndFunction
Function SM_CountChannels(wav)
Return PeekByte(wav,0)
EndFunction
Function SM_GetChannelRawData(wav,channel = 1)
channel - 1
length = PeekInt(wav,12)
mem = MakeMEMBlock(length*4)
i2 = 0
If PeekByte(wav,0) = 1 Then
For i = 0 To length
PokeInt mem,i2,PeekInt(wav,i*4)
i2 + 4
Next i
Else
For i = channel*4 To length
PokeInt mem,i2,PeekInt(wav,i*8)
i2 + 4
Next i
EndIf
Return mem
EndFunction
Function SM_SetChannelRawData(wav,wavdata,channel = 1)
channel - 1
lastlength = PeekInt(wav,12)
length = MEMBlockSize(wavdata)
channels = PeekByte(wav,0)
If lastlength < length Then
If channels = 1 Then
ResizeMEMBlock wav,16+length*4
Else
ResizeMEMBlock wav,16+length*8
EndIf
lastlength = length
EndIf
plus = channels * 4
index = 16 + channel*4
For i = 0 To length
PokeInt wav,index,PeekInt(wavdata,i*4)
index = index + plus
Next i
PokeInt wav,12,lastlength
EndFunction
Function SM_GetLength(wav)
Return Float(PeekInt(wav,12))/Float(PeekInt(wav,1))
EndFunction
Function SM_WriteWav(path$,samplerate,data1,data2=0)
datalen = MEMBlockSize(data1)
If data2 <> 0 Then channels = 2 Else channels = 1
f = OpenToWrite(path$)
WriteByte f,$52 : WriteByte f,$49 : WriteByte f,$46 : WriteByte f,$46//RIFF
WriteInt f,4 + (8+16)+(8 + datalen/4 * channels * 4)
WriteByte f,$57 : WriteByte f,$41 : WriteByte f,$56 : WriteByte f,$45//WAVE
WriteByte f,$66 : WriteByte f,$6d : WriteByte f,$74 : WriteByte f,$20 //FMT
WriteInt f,16
WriteShort f,1
WriteShort f,channels
WriteInt f,samplerate
WriteInt f,samplerate * channels * 4
WriteShort f,channels * 4
WriteShort f,32
WriteByte f,$64 : WriteByte f,$61 : WriteByte f,$74 : WriteByte f,$61 //Data
WriteInt f,datalen/4*channels*4
If channels = 1 Then
For i = 0 To datalen-4 Step 4
WriteInt f,PeekInt(data1,i)
Next i
ElseIf channels = 2
For i = 0 To datalen-4 Step 4
WriteInt f,PeekInt(data1,i)
WriteInt f,PeekInt(data2,i)
Next i
EndIf
CloseFile f
EndFunction
Function SM_SaveWav(path$,wav)
f = OpenToWrite(path$)
datalen = PeekInt(wav,12)
channels = PeekByte(wav,0)
samplerate = PeekInt(wav,1)
WriteByte f,$52 : WriteByte f,$49 : WriteByte f,$46 : WriteByte f,$46//RIFF
WriteInt f,4 + (8+16)+(8 + datalen/4 * channels * 4)
WriteByte f,$57 : WriteByte f,$41 : WriteByte f,$56 : WriteByte f,$45//WAVE
WriteByte f,$66 : WriteByte f,$6d : WriteByte f,$74 : WriteByte f,$20 //FMT
WriteInt f,16
WriteShort f,1
WriteShort f,channels
WriteInt f,samplerate
WriteInt f,samplerate * channels * 4
WriteShort f,channels * 4
WriteShort f,32
WriteByte f,$64 : WriteByte f,$61 : WriteByte f,$74 : WriteByte f,$61 //Data
WriteInt f,datalen/4*channels*4
For i = 0 To datalen*channels-4 Step 4
WriteInt f,PeekInt(wav,i+16)
Next i
CloseFile f
EndFunction
//Playlist by Latexi95
Type SM_PlayList
Field Path As String
Field Volume#
Field MaxVolume#
Field PlayStartTime
Field Lenght
Field Music
Field ChangeMultiplier#
Field Section As Byte
Field LastUpdate
Field Class As String
EndType
Const SM_S_QuietDown = 0
Const SM_S_NormalPlay = 1
Const SM_S_Crescendo = 2
Const SM_QuietDown = 5000//ms
Global SM_CurrentMusic,SM_NextMusic,SM_CurrentClass As String,SM_Continue
SM_Continue = True
Function SM_AddToPlayList(path$,class As String,lenght,Volume = 100)
If FileExists(path$) = False Then MakeError "SM_AddToPlayList failed! File: "+path$+" not found."
Music.SM_PlayList = New (SM_PlayList)
Music\Path$ = path$
Music\Class$ = Lower(class$)
Music\MaxVolume = Volume
Music\Volume# = 0
Music\Lenght = lenght
Music\Section = SM_S_NormalPlay
i = ConvertToInteger(Music)
If SM_CurrentMusic = 0 Then
SM_CurrentMusic = i
SM_CurrentClass As String = Lower(class$)
EndIf
Return i
EndFunction
Function SM_UpdateMusic()
If SM_CurrentMusic <> 0 Then
Music.SM_PlayList = ConvertToType(SM_CurrentMusic)
If Music\Music = 0 Then
Music\Music = PlaySound(Music\Path$,Music\Volume#)
Music\PlayStartTime = Timer()
Music\ChangeMultiplier# = (Music\MaxVolume# - Music\Volume#)/SM_QuietDown
Music\LastUpdate = Timer()
EndIf
If Music\PlayStartTime + SM_QuietDown > Timer() And Music\Section = SM_S_NormalPlay Then
Music\Section = SM_S_Crescendo
ElseIf (Music\PlayStartTime+Music\Lenght-SM_QuietDown) < Timer() And Music\Section = SM_S_NormalPlay
Music\Section = SM_S_QuietDown
EndIf
If Music\Section = SM_S_Crescendo Then
Music\Volume# = Music\Volume# + (Timer()-Music\LastUpdate)*Music\ChangeMultiplier#
Changed = True
If Music\Volume# > Music\MaxVolume# Then Music\Volume = Music\MaxVolume : Music\Section = SM_S_NormalPlay
ElseIf Music\Section = SM_S_QuietDown Then
Music\Volume# = Music\Volume# - (Timer()-Music\LastUpdate)*Music\ChangeMultiplier#
Changed = True
If Music\Volume# < 0 Then
StopSound Music\Music
Music\Music = 0
Music\Volume# = 0
Music\Section = SM_S_NormalPlay
If SM_NextMusic = 0 Then
m.SM_PlayList = Music
Repeat
If After(m) <> NULL Then
m.SM_PlayList = After(m)
Else
m.SM_PlayList = First(SM_PlayList)
EndIf
If m\Class$ = SM_CurrentClass Then
SM_NextMusic = ConvertToInteger(m)
Exit
ElseIf ConvertToInteger(m) = ConvertToInteger(Music)
MakeError "SM_UpdateMusic failed! Can't find songs in class '"+SM_CurrentClass+"'."
EndIf
Forever
EndIf
If SM_Continue = True Then
SM_CurrentMusic = SM_NextMusic
SM_NextMusic = 0
Else
SM_CurrentMusic = 0
EndIf
Changed = False
EndIf
EndIf
If Changed = True Then SetSound Music\Music,OFF,Music\Volume#
Music\LastUpdate = Timer()
ElseIf SM_Continue = True
SM_CurrentMusic = SM_NextMusic
SM_NextMusic = 0
EndIf
EndFunction
Function SM_ChangeMusic(music,class = True)
SM_NextMusic = music
m.SM_PlayList = music
If class = True Then SM_CurrentClass = m\Class
m.SM_PlayList = ConvertToType(SM_CurrentMusic)
m\Section = SM_S_QuietDown
EndFunction
Function SM_PlayNext()
m.SM_PlayList = ConvertToType(SM_CurrentMusic)
m\Section = SM_S_QuietDown
EndFunction
Function SM_StopMusic()
SM_Continue = False
m.SM_PlayList = ConvertToType(SM_CurrentMusic)
m\Section = SM_S_QuietDown
EndFunction
Function SM_ContinuePlaying()
SM_Continue = True
EndFunction
Function SM_SetClass(class$)
If Int(class$) = 0 Then
SM_CurrentClass = class$
Else
m.SM_PlayList = ConvertToType(Int(class))
SM_CurrentClass = m\Class$
EndIf
m.SM_PlayList = ConvertToType(SM_CurrentMusic)
m\Section = SM_S_QuietDown
EndFunction
Code: Select all
SCREEN 1200,600
Function SM_WriteWav(path$,samplerate,data1,data2=0)
datalen = MEMBlockSize(data1)
If data2 <> 0 Then channels = 2 Else channels = 1
f = OpenToWrite(path$)
WriteByte f,$52 : WriteByte f,$49 : WriteByte f,$46 : WriteByte f,$46//RIFF
WriteInt f,4 + (8+16)+(8 + datalen/4 * channels * 4)
WriteByte f,$57 : WriteByte f,$41 : WriteByte f,$56 : WriteByte f,$45//WAVE
WriteByte f,$66 : WriteByte f,$6d : WriteByte f,$74 : WriteByte f,$20 //FMT
WriteInt f,16
WriteShort f,1
WriteShort f,channels
WriteInt f,samplerate
WriteInt f,samplerate * channels * 4
WriteShort f,channels * 4
WriteShort f,32
WriteByte f,$64 : WriteByte f,$61 : WriteByte f,$74 : WriteByte f,$61 //Data
WriteInt f,datalen/4*channels*4
If channels = 1 Then
For i = 0 To datalen-4 Step 4
WriteInt f,PeekInt(data1,i)
Next i
ElseIf channels = 2
For i = 0 To datalen-4 Step 4
WriteInt f,PeekInt(data1,i)
WriteInt f,PeekInt(data2,i)
Next i
EndIf
CloseFile f
EndFunction
sample=MakeMEMBlock(5001)
bt=0
For i=1 To 5000
PokeByte sample, i, bt
bt=bt+1
bt=WrapAngle(bt)
Next i
SM_WriteWav("mysample.wav", 5000, sample)
DeleteMEMBlock sample
PlaySound "mysample.wav"
Print "Playing"
WaitKey
Eikös tuo 360:lla kertominen tulisi Sin-funktion jälkeen??? Vai olenko ihan hakoteillä? 360 ilmaisee täyttä kierrosta (yhtä aaltoa), joten sitä ei voida saavuttaa jos kertominen tapahtuu funktion sisällä (johtuen siitä että sinifunktio osaa palauttaa arvon vain -1:n ja 1:n väliltä).atomimalli wrote:Saat aallonpituuden sampleina muistaakseni kaavalla samplerate/taajuus. Siitä vaikka siniaallon jotenkin näin: sin(i*360*samplerate/taajuus)+1 siitä taas kanttiaallon int(sin(i*360*samplerate/taajuus)*0.5+0.5)
Nuo pitää tietysti myös skaalata sopivalle voimakkuudelle.
Korjatkoon joku jos meni pieleen.
Code: Select all
SCREEN 1280,772
SCREEN 1280,772,0,2
Function SM_WriteWav(path$,samplerate,data1,data2=0)
datalen = MEMBlockSize(data1)
If data2 <> 0 Then channels = 2 Else channels = 1
f = OpenToWrite(path$)
WriteByte f,$52 : WriteByte f,$49 : WriteByte f,$46 : WriteByte f,$46//RIFF
WriteInt f,4 + (8+16)+(8 + datalen/4 * channels * 4)
WriteByte f,$57 : WriteByte f,$41 : WriteByte f,$56 : WriteByte f,$45//WAVE
WriteByte f,$66 : WriteByte f,$6d : WriteByte f,$74 : WriteByte f,$20 //FMT
WriteInt f,16
WriteShort f,1
WriteShort f,channels
WriteInt f,samplerate
WriteInt f,samplerate * channels * 4
WriteShort f,channels * 4
WriteShort f,32
WriteByte f,$64 : WriteByte f,$61 : WriteByte f,$74 : WriteByte f,$61 //Data
WriteInt f,datalen/4*channels*4
If channels = 1 Then
For i = 0 To datalen-4 Step 4
WriteInt f,PeekInt(data1,i)
Next i
ElseIf channels = 2
For i = 0 To datalen-4 Step 4
WriteInt f,PeekInt(data1,i)
WriteInt f,PeekInt(data2,i)
Next i
EndIf
CloseFile f
EndFunction
slen=2000000
sample=MakeMEMBlock(slen+1)
sample2=MakeMEMBlock(slen+1)
bt#=0
times=10
cur=-100
For o=1 To times
For i#=o*slen/times-slen/times To o*slen/times
cur +1
lus#=Rand(0,255)
PokeByte sample, Int(i), bt
If cur > 1 Then
PokeByte sample2, cur,bt
EndIf
bt=bt+lus
Next i#
Text 0,0,"Prosessoidaan avaruushuminaa... "+o+"0 %" : DrawScreen
Next o
wavelen=255*2
Text 0,0,"Renderöidään avaruushuminaa..." : DrawScreen
SM_WriteWav("mysample.wav", 1000, sample, sample2)
DeleteMEMBlock sample
Text 0,0,"Soitetaan avaruushuminaa..." : DrawScreen
b=PlaySound("mysample.wav")
Text 0,0,"Valmistellaan avaruus..." : DrawScreen
Type piste
Field x
Field y
Field k#
Field n#
Field h#
EndType
For i=1 To 500
p.piste=New(piste)
p\x=Rand(0,ScreenWidth())
p\y=Rand(0,ScreenHeight())
p\k=Rand(0,360)
p\h = -1
Next i
majorkulma#=0
Repeat
majorkulma=majorkulma+0.5
For i=1 To 5
p.piste=New(piste)
p\x=ScreenWidth()/2+Rand(-2,2)
p\y=ScreenHeight()/2+Rand(-2,2)
p\k=Rand(0,360)
p\h = Rand(1,400)
p\h=p\h/100
Next i
For p.piste = Each piste
If p\h <> -1 Then
Dot p\x,p\y//, p\n+p\h-5
p\x=p\x+Cos(p\k)*p\n*p\h
p\y=p\y-Sin(p\k)*p\n*p\h
p\n=p\n+0.3
p\h=p\h+p\h*0.2
Else
Dot p\x,p\y
p\x=p\x+Cos(p\k)+Cos(majorkulma)
p\y=p\y-Sin(p\k)+Sin(majorkulma)
p\n=p\n+0.003
If p\x < 0 Then
p\x=ScreenWidth()
EndIf
EndIf
If p\x > ScreenWidth() Or p\x < 0 Or p\y > ScreenHeight() Or p\y < 0 Then Delete p
Next p
If SoundPlaying(b)=0 Then b=PlaySound("mysample.wav")
DrawScreen
Forever
Eli jos volyymi on 100 niin kerronkin 255 :llä, vai?koodaaja wrote:Olet jokseenkin hakoteilla. Sini palauttaa väliltä [-1;1] olevan arvon kulmasta, jonka saat oikeaksi kertomalla 360:llä eli yhdellä kierroksella. Vain sinin parametri vaikuttaa sen taajuuteen, sinifunktion itsensä kertominen taas vaikuttaa amplitudiin eli aallon suuruuteen; äänen voimakkuuteen.
Varmaankin 127.5+127.5*sin(parametrit), jotta saat aallon nollasta 255:een.DJ-Filbe wrote:Eli jos volyymi on 100 niin kerronkin 255 :llä, vai?koodaaja wrote:Olet jokseenkin hakoteilla. Sini palauttaa väliltä [-1;1] olevan arvon kulmasta, jonka saat oikeaksi kertomalla 360:llä eli yhdellä kierroksella. Vain sinin parametri vaikuttaa sen taajuuteen, sinifunktion itsensä kertominen taas vaikuttaa amplitudiin eli aallon suuruuteen; äänen voimakkuuteen.