diff options
author | Ian C <ianc@noddybox.co.uk> | 2020-06-12 20:08:53 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2020-06-12 20:08:53 +0000 |
commit | 9ec3d17d3fe6ae3f1d1f8fa38f0edcd5197ea048 (patch) | |
tree | 1cb9ed529e13bf4cfee6a311d7bdd160294b1c8a | |
parent | 9727c96cfdf6a533acc2956ab85e414f003c633e (diff) |
-rw-r--r-- | SFXGen.bmx | 515 | ||||
-rw-r--r-- | types.bmx | 816 |
2 files changed, 666 insertions, 665 deletions
@@ -1,257 +1,258 @@ -' SFX Generator -' -' Copyright (C) 2006 Ian Cowburn (ianc@noddybox.co.uk) -' -' This program is free software; you can redistribute it and/or modify -' it under the terms of the GNU General Public License as published by -' the Free Software Foundation; either version 2 of the License, or -' (at your option) any later version. -' -' This program is distributed in the hope that it will be useful, -' but WITHOUT ANY WARRANTY; without even the implied warranty of -' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -' GNU General Public License for more details. -' -' You should have received a copy of the GNU General Public License -' along with this program; if not, write to the Free Software -' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -' -' ------------------------------------------------------------------------- -' -' $Id$ -' -SuperStrict -Import brl.audio -Import brl.audiosample - -Import noddybox.mwidget -Import "types.bmx" - -Global top:SFXGenApp -Global menu:TMMenu -Global path:String="" - -Type SFXGenApp Extends TMWindow - Field env:EnvelopeControl - Field wave:WaveControl - Field sample:SampleControl - Field labels:TList - Field samples:TMSlider - Field wavefreq:TMSlider - - Function Init:SFXGenApp() - Local o:SFXGenApp=SFXGenApp(New SFXGenApp.Create("SFX Generator",100,100,785,500,Null, .. - WINDOW_TITLEBAR|.. - WINDOW_MENU|.. - WINDOW_CLIENTCOORDS|.. - WINDOW_HIDDEN|.. - WINDOW_ACCEPTFILES)) - - o.labels=CreateList() - - o.labels.AddLast(New TMLabel.Create("Envelope:",5,5,100,15,o)) - o.env=EnvelopeControl(New EnvelopeControl.Create(5,20,400,255,o)) - - o.labels.AddLast(New TMLabel.Create("Wave:",420,5,100,15,o)) - o.wave=WaveControl(New WaveControl.Create(420,20,360,255,o)) - - o.labels.AddLast(New TMLabel.Create("Sample:",5,300,100,15,o)) - o.sample=SampleControl(New SampleControl.Create(5,320,775,100,o)) - - o.samples=New TMSlider.Create(5,280,15,15,o,SLIDER_STEPPER) - o.samples.SetRange(1,100000) - - o.NewSFX() - - Return o - End Function - - Method OnClose() - 'closed=Confirm("Really quit?") - closed=True - End Method - - Method OnDrop(p:String) - Load(p) - End Method - - Method Load:Int(p:String) - Local s:TStream=ReadStream(p) - - If Not s - Return False - EndIf - - s=BigEndianStream(s) - - Local m:String=ReadString(s,MAGIC.length) - - If m<>MAGIC - CloseStream(s) - Return False - EndIf - - env.FromStream(s) - wave.FromStream(s) - - path=p - - CloseStream(s) - - Text("SFX Generator -- " + p) - - Return True - End Method - - Method Save:Int(p:String) - Local s:TStream=WriteStream(p) - - If Not s - Return False - EndIf - - s=BigEndianStream(s) - - WriteString(s,MAGIC) - - env.ToStream(s) - wave.ToStream(s) - - path=p - - CloseStream(s) - - Text("SFX Generator -- " + p) - - Return True - End Method - - Method NewSFX() - env.Clear() - wave.Clear() - sample.Clear() - path="" - Text("SFX Generator -- [untitled]") - End Method - - Method Generate() - sample.Generate(env.GetPointList(),wave.GetPointList()) - End Method - - Method Play() - sample.Play() - End Method -End Type - -Function MenuCallback(m:TMMenu, p:String, t:Object) - Select p - Case "/file/new" - menu.Enable("/file/save",False) - menu.Enable("/file/savewav",False) - menu.Enable("/sample/play",False) - top.NewSFX() - - Case "/file/open" - Local fn:String=RequestFile("Select SFX to open","SFX Files:sfx;All Files:*",False,path$) - - If fn.length - If ExtractExt(fn).length=0 - fn:+".sfx" - EndIf - If top.Load(fn$) - menu.Enable("/file/save",True) - End If - EndIf - - Case "/file/save" - top.Save(path$) - - Case "/file/saveas" - Local fn:String=RequestFile("Select SFX to save","SFX Files:sfx;All Files:*",True,path$) - If fn.length - If ExtractExt(fn).length=0 - fn:+".sfx" - EndIf - If top.Save(fn$) - menu.Enable("/file/save",True) - End If - EndIf - - Case "/file/savewav" - menu.Enable("/file/save",True) - - Case "/file/quit" - top.OnClose() - - Case "/wave/flat" - top.wave.SetWave(WaveControl.EMPTY) - Case "/wave/sine" - top.wave.SetWave(WaveControl.SINE) - Case "/wave/square" - top.wave.SetWave(WaveControl.SQUARE) - Case "/wave/triangle" - top.wave.SetWave(WaveControl.TRIANGLE) - Case "/wave/saw" - top.wave.SetWave(WaveControl.SAW) - Case "/wave/noise" - top.wave.SetWave(WaveControl.NOISE) - - Case "/sample/generate" - top.Generate() - menu.Enable("/sample/play",True) - Case "/sample/play" - top.Play() - - Case "/help/about" - Notify("SFX Generator~nCopyright (c) 2006 Ian Cowburn~n~n" .. - + "This program is distributed in the hope that it will be useful,~n" .. - + "but WITHOUT ANY WARRANTY; without even the implied warranty of~n" .. - + "MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE. See the~n" .. - + "GNU General Public License for more details.") - -?Debug - Case "/help/break" - DebugStop -? - End Select -End Function - -top=SFXGenApp.Init() - -menu=New TMMenu.CreateWindowMenu(top) - -menu.Set("/file","File") -menu.Set("/file/new","New",MenuCallback) -menu.Set("/file/open","Open",MenuCallback) -menu.Set("/file/save","Save",MenuCallback) -menu.Set("/file/saveas","Save as...",MenuCallback) -menu.Set("/file/savewav","Save sample...",MenuCallback) -menu.Set("/file/quit","Quit",MenuCallback) - -menu.Set("/wave","Wave Form") -menu.Set("/wave/flat","Flat",MenuCallback) -menu.Set("/wave/sine","Sine",MenuCallback) -menu.Set("/wave/square","Square",MenuCallback) -menu.Set("/wave/triangle","Triangle",MenuCallback) -menu.Set("/wave/saw","Sawtooth",MenuCallback) -menu.Set("/wave/noise","Noise",MenuCallback) - -menu.Set("/sample","Sample") -menu.Set("/sample/generate","Generate",MenuCallback) -menu.Set("/sample/play","Play",MenuCallback) - -menu.Set("/help","Help") -menu.Set("/help/about","About",MenuCallback) - -?Debug -menu.Set("/help/break","Debug Break",MenuCallback) -? - -menu.Enable("/file/save",False) -menu.Enable("/file/savewav",False) -menu.Enable("/sample/play",False) - -top.Hidden(False) -MWidgetMainLoop(top) - -End +' SFX Generator
+'
+' Copyright (C) 2006 Ian Cowburn (ianc@noddybox.co.uk)
+'
+' This program is free software; you can redistribute it and/or modify
+' it under the terms of the GNU General Public License as published by
+' the Free Software Foundation; either version 2 of the License, or
+' (at your option) any later version.
+'
+' This program is distributed in the hope that it will be useful,
+' but WITHOUT ANY WARRANTY; without even the implied warranty of
+' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+' GNU General Public License for more details.
+'
+' You should have received a copy of the GNU General Public License
+' along with this program; if not, write to the Free Software
+' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+'
+' -------------------------------------------------------------------------
+'
+' $Id$
+'
+SuperStrict
+Import brl.audio
+Import brl.audiosample
+Import maxgui.drivers
+
+Import noddybox.mwidget
+Import "types.bmx"
+
+Global top:SFXGenApp
+Global menu:TMMenu
+Global path:String=""
+
+Type SFXGenApp Extends TMWindow
+ Field env:EnvelopeControl
+ Field wave:WaveControl
+ Field sample:SampleControl
+ Field labels:TList
+ Field samples:TMSlider
+ Field wavefreq:TMSlider
+
+ Function Init:SFXGenApp()
+ Local o:SFXGenApp=SFXGenApp(New SFXGenApp.Create("SFX Generator",100,100,785,500,Null, ..
+ WINDOW_TITLEBAR|..
+ WINDOW_MENU|..
+ WINDOW_CLIENTCOORDS|..
+ WINDOW_HIDDEN|..
+ WINDOW_ACCEPTFILES))
+
+ o.labels=CreateList()
+
+ o.labels.AddLast(New TMLabel.Create("Envelope:",5,5,100,15,o))
+ o.env=EnvelopeControl(New EnvelopeControl.Create(5,20,400,255,o))
+
+ o.labels.AddLast(New TMLabel.Create("Wave:",420,5,100,15,o))
+ o.wave=WaveControl(New WaveControl.Create(420,20,360,255,o))
+
+ o.labels.AddLast(New TMLabel.Create("Sample:",5,300,100,15,o))
+ o.sample=SampleControl(New SampleControl.Create(5,320,775,100,o))
+
+ o.samples=New TMSlider.Create(5,280,15,15,o,SLIDER_STEPPER)
+ o.samples.SetRange(1,100000)
+
+ o.NewSFX()
+
+ Return o
+ End Function
+
+ Method OnClose:Int()
+ 'closed=Confirm("Really quit?")
+ closed=True
+ End Method
+
+ Method OnDrop:Int(p:String)
+ Load(p)
+ End Method
+
+ Method Load:Int(p:String)
+ Local s:TStream=ReadStream(p)
+
+ If Not s
+ Return False
+ EndIf
+
+ s=BigEndianStream(s)
+
+ Local m:String=ReadString(s,MAGIC.length)
+
+ If m<>MAGIC
+ CloseStream(s)
+ Return False
+ EndIf
+
+ env.FromStream(s)
+ wave.FromStream(s)
+
+ path=p
+
+ CloseStream(s)
+
+ Text("SFX Generator -- " + p)
+
+ Return True
+ End Method
+
+ Method Save:Int(p:String)
+ Local s:TStream=WriteStream(p)
+
+ If Not s
+ Return False
+ EndIf
+
+ s=BigEndianStream(s)
+
+ WriteString(s,MAGIC)
+
+ env.ToStream(s)
+ wave.ToStream(s)
+
+ path=p
+
+ CloseStream(s)
+
+ Text("SFX Generator -- " + p)
+
+ Return True
+ End Method
+
+ Method NewSFX()
+ env.Clear()
+ wave.Clear()
+ sample.Clear()
+ path=""
+ Text("SFX Generator -- [untitled]")
+ End Method
+
+ Method Generate()
+ sample.Generate(env.GetPointList(),wave.GetPointList())
+ End Method
+
+ Method Play()
+ sample.Play()
+ End Method
+End Type
+
+Function MenuCallback:Int(m:TMMenu, p:String, t:Object)
+ Select p
+ Case "/file/new"
+ menu.Enable("/file/save",False)
+ menu.Enable("/file/savewav",False)
+ menu.Enable("/sample/play",False)
+ top.NewSFX()
+
+ Case "/file/open"
+ Local fn:String=RequestFile("Select SFX to open","SFX Files:sfx;All Files:*",False,path$)
+
+ If fn.length
+ If ExtractExt(fn).length=0
+ fn:+".sfx"
+ EndIf
+ If top.Load(fn$)
+ menu.Enable("/file/save",True)
+ End If
+ EndIf
+
+ Case "/file/save"
+ top.Save(path$)
+
+ Case "/file/saveas"
+ Local fn:String=RequestFile("Select SFX to save","SFX Files:sfx;All Files:*",True,path$)
+ If fn.length
+ If ExtractExt(fn).length=0
+ fn:+".sfx"
+ EndIf
+ If top.Save(fn$)
+ menu.Enable("/file/save",True)
+ End If
+ EndIf
+
+ Case "/file/savewav"
+ menu.Enable("/file/save",True)
+
+ Case "/file/quit"
+ top.OnClose()
+
+ Case "/wave/flat"
+ top.wave.SetWave(WaveControl.EMPTY)
+ Case "/wave/sine"
+ top.wave.SetWave(WaveControl.SINE)
+ Case "/wave/square"
+ top.wave.SetWave(WaveControl.SQUARE)
+ Case "/wave/triangle"
+ top.wave.SetWave(WaveControl.TRIANGLE)
+ Case "/wave/saw"
+ top.wave.SetWave(WaveControl.SAW)
+ Case "/wave/noise"
+ top.wave.SetWave(WaveControl.NOISE)
+
+ Case "/sample/generate"
+ top.Generate()
+ menu.Enable("/sample/play",True)
+ Case "/sample/play"
+ top.Play()
+
+ Case "/help/about"
+ Notify("SFX Generator~nCopyright (c) 2006 Ian Cowburn~n~n" ..
+ + "This program is distributed in the hope that it will be useful,~n" ..
+ + "but WITHOUT ANY WARRANTY; without even the implied warranty of~n" ..
+ + "MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE. See the~n" ..
+ + "GNU General Public License for more details.")
+
+?Debug
+ Case "/help/break"
+ DebugStop
+?
+ End Select
+End Function
+
+top=SFXGenApp.Init()
+
+menu=New TMMenu.CreateWindowMenu(top)
+
+menu.Set("/file","File")
+menu.Set("/file/new","New",MenuCallback)
+menu.Set("/file/open","Open",MenuCallback)
+menu.Set("/file/save","Save",MenuCallback)
+menu.Set("/file/saveas","Save as...",MenuCallback)
+menu.Set("/file/savewav","Save sample...",MenuCallback)
+menu.Set("/file/quit","Quit",MenuCallback)
+
+menu.Set("/wave","Wave Form")
+menu.Set("/wave/flat","Flat",MenuCallback)
+menu.Set("/wave/sine","Sine",MenuCallback)
+menu.Set("/wave/square","Square",MenuCallback)
+menu.Set("/wave/triangle","Triangle",MenuCallback)
+menu.Set("/wave/saw","Sawtooth",MenuCallback)
+menu.Set("/wave/noise","Noise",MenuCallback)
+
+menu.Set("/sample","Sample")
+menu.Set("/sample/generate","Generate",MenuCallback)
+menu.Set("/sample/play","Play",MenuCallback)
+
+menu.Set("/help","Help")
+menu.Set("/help/about","About",MenuCallback)
+
+?Debug
+menu.Set("/help/break","Debug Break",MenuCallback)
+?
+
+menu.Enable("/file/save",False)
+menu.Enable("/file/savewav",False)
+menu.Enable("/sample/play",False)
+
+top.Hidden(False)
+MWidgetMainLoop(top)
+
+End
@@ -1,408 +1,408 @@ -' SFX Generator -' -' Copyright (C) 2006 Ian Cowburn (ianc@noddybox.co.uk) -' -' This program is free software; you can redistribute it and/or modify -' it under the terms of the GNU General Public License as published by -' the Free Software Foundation; either version 2 of the License, or -' (at your option) any later version. -' -' This program is distributed in the hope that it will be useful, -' but WITHOUT ANY WARRANTY; without even the implied warranty of -' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -' GNU General Public License for more details. -' -' You should have received a copy of the GNU General Public License -' along with this program; if not, write to the Free Software -' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -' -' ------------------------------------------------------------------------- -' -' $Id$ -' -SuperStrict -Import noddybox.mwidget -Import noddybox.algorithm -Import brl.audio -Import brl.audiosample - -Const MAGIC:String="SFXV0001" - -Type TPoint - Field x:Int - Field y:Int - - Function Create:TPoint(x:Int, y:Int) - Local o:TPoint=New TPoint - o.x=x - o.y=y - Return o - End Function - - Method Draw(drag:Int) - If drag - SetColor(255,255,0) - Else - SetColor(220,220,220) - EndIf - DrawRect(x-2,y-2,5,5) - End Method - - Method InRange:Int(mx:Int, my:Int) - Return Abs(mx-x)<3 And Abs(my-y)<3 - End Method - - Method ToStream(s:TStream) - WriteInt(s,x) - WriteInt(s,y) - End Method - - Function FromStream:TPoint(s:TStream) - Local x:Int=ReadInt(s) - Local y:Int=ReadInt(s) - Return Create(x,y) - End Function -End Type - -Type TPointList - Field arr:Object[] - Field link:TLink - Field x:Int - Field p:Int - Field cur:TList - Field depth:Int - - Function Create:TPointList(l:TList, depth:Int) - Local o:TPointList=New TPointList - o.arr=l.ToArray() - o.depth=depth-1 - o.Reset() - Return o - End Function - - Method NextLine() - p:+1 - If p=arr.length - Return - EndIf - Local p1:TPoint=TPoint(arr[p-1]) - Local p2:TPoint=TPoint(arr[p]) - cur=DoLine(p1.x,p1.y,p2.x,p2.y) - End Method - - Method AtEnd:Int() - Return p=arr.length - End Method - - Method Point:Double() - Local p:TAlgoPoint=TAlgoPoint(cur.RemoveFirst()) - - If Not cur.Count() - NextLine() - EndIf - - Return Double(depth-p.y)/depth - End Method - - Method Reset() - x=0 - p=0 - NextLine() - End Method -End Type - -Type EnvelopeControl Extends TMCanvas - Field points:TList - Field drag:TPoint - Field pleft:TPoint - Field pright:TPoint - Field mx:Int - Field my:Int - - Method New() - points=CreateList() - drag=Null - pleft=Null - pright=Null - End Method - - Function Init:TMCanvas(x:Int, y:Int, w:Int, h:Int, p:TMWidget) - Local o:TMCanvas=New EnvelopeControl.Create(x,y,w,h,p) - Return o - End Function - - Method Clear() - points.Clear() - points.AddLast(TPoint.Create(0,Height()/2)) - points.AddLast(TPoint.Create(Width()-1,Height()/2)) - End Method - - Method ToStream(s:TStream) - WriteInt(s,points.Count()) - For Local p:TPoint=EachIn points - p.ToStream(s) - Next - End Method - - Method FromStream(s:TStream) - Local c:Int=ReadInt(s) - points.Clear() - For Local f:Int=1 To c - points.AddLast(TPoint.FromStream(s)) - Next - Draw() - End Method - - Method LocatePoint() - pleft=Null - Local l:TLink=points.FirstLink() - - While l - Local p:TPoint=TPoint(l.Value()) - - If p.InRange(mx,my) - drag=p - l=l.NextLink() - If l - pright=TPoint(l.Value()) - EndIf - Return - End If - - If p.x>mx - drag=TPoint.Create(mx,my) - pright=p - points.InsertBeforeLink(drag,l) - Return - EndIf - - pleft=p - l=l.NextLink() - Wend - - drag=Null - pleft=Null - pright=Null - End Method - - Method Draw() - SetupGraphics() - - Cls() - - Local prev:TPoint=Null - - SetColor(180,180,180) - - For Local p:TPoint=EachIn points - If prev - DrawLine(prev.x,prev.y,p.x,p.y) - EndIf - prev=p - Next - - For Local p:TPoint=EachIn points - p.Draw(p=drag) - Next - - Flip - End Method - - Method OnRedraw() - Draw() - End Method - - Method OnMouseEnter() - SetPointer(POINTER_CROSS) - End Method - - Method OnMouseLeave() - SetPointer(POINTER_DEFAULT) - End Method - - Method OnButtonDown(b:Int) - If Not drag - LocatePoint() - Draw() - EndIf - End Method - - Method OnButtonUp(b:Int) - If drag - If pleft And pleft.InRange(drag.x,drag.y) - points.Remove(drag) - ElseIf pright And pright.InRange(drag.x,drag.y) - points.Remove(drag) - End If - drag=Null - pleft=Null - pright=Null - Draw() - EndIf - End Method - - Method OnMouseMove(x:Int, y:Int) - mx=x - my=y - - If drag - If pleft And pright - drag.x=Max(pleft.x,Min(pright.x,mx)) - ElseIf pleft - drag.x=Width()-1 - Else - drag.x=0 - EndIf - - drag.y=Max(0,Min(Height()-1,my)) - - Draw() - EndIf - End Method - - Method OnMouseWheel(b:Int) - End Method - - Method OnKeyDown(b:Int) - End Method - - Method OnKeyUp(b:Int) - End Method - - Method OnKey(b:Int) - End Method - - Method GetPointList:TPointList() - Return TPointList.Create(points,Height()) - End Method -End Type - -Type WaveControl Extends EnvelopeControl - Const EMPTY:Int=0 - Const SINE:Int=1 - Const SQUARE:Int=2 - Const SAW:Int=3 - Const TRIANGLE:Int=4 - Const NOISE:Int=5 - - Method SetWave(t:Int) - Select t - Case EMPTY - Clear() - - Case SINE - points.Clear() - For Local x:Int=0 Until Width() - points.AddLast(TPoint.Create(x,Height()/2+Sin(x)*((Height()-5)/2))) - Next - - - Case SQUARE - points.Clear() - points.AddLast(TPoint.Create(0,0)) - points.AddLast(TPoint.Create(Width()/2,0)) - points.AddLast(TPoint.Create(Width()/2,Height()-1)) - points.AddLast(TPoint.Create(Width()-1,Height()-1)) - - - Case SAW - points.Clear() - points.AddLast(TPoint.Create(0,Height()-1)) - points.AddLast(TPoint.Create(Width()-1,0)) - - Case TRIANGLE - points.Clear() - points.AddLast(TPoint.Create(0,Height()-1)) - points.AddLast(TPoint.Create(Width()/2,0)) - points.AddLast(TPoint.Create(Width()-1,Height()-1)) - - Case NOISE - points.Clear() - For Local x:Int=0 Until Width() - points.AddLast(TPoint.Create(x,Rand(0,Height()-1))) - Next - - Default - Clear() - End Select - - Draw() - End Method -End Type - -Type SampleControl Extends TMCanvas - Field sample:TAudioSample - Field sound:TSound - - Method Draw() - SetupGraphics() - Cls - If sample - Local s:Int=Max(1,sample.length/Width()) - Local p:Int=0 - Local x:Int=0 - - SetColor(255,255,255) - - While p<sample.length - 'Plot(x,Height()-1-sample.samples[p]) - DrawLine(x,Height()-1,x,Height()-1-sample.samples[p]/3) - p:+s - x:+1 - Wend - EndIf - Flip - End Method - - Method OnRedraw() - Draw() - End Method - - Method Generate(env:TPointList, wave:TPointList) - Const PERSTEP:Int=100 - Const PERWAV:Int=1 - Const WAVSPD:Int=10 - sample=CreateAudioSample(400*PERSTEP,44100,SF_MONO8) - - Local p:Int=0 - Local ep:Double - Local wp:Double - Local wd:Int=PERWAV - - ep=env.Point() - wp=wave.Point() - While Not env.AtEnd() And p<400*PERSTEP - For Local f:Int=0 Until PERSTEP - If Not wd - wd=PERWAV - For Local f:Int=0 Until WAVSPD - If wave.AtEnd() - wave.Reset() - EndIf - - wp=wave.Point() - Next - Else - wd:-1 - EndIf - - sample.samples[p]=(ep*wp)*255 - p:+1 - Next - ep=env.Point() - Wend - Draw() - End Method - - Method Play() - sound:TSound=LoadSound(sample,False) - PlaySound(sound) - End Method - - Method Clear() - sample=Null - sound=Null - Draw() - End Method -End Type - +' SFX Generator
+'
+' Copyright (C) 2006 Ian Cowburn (ianc@noddybox.co.uk)
+'
+' This program is free software; you can redistribute it and/or modify
+' it under the terms of the GNU General Public License as published by
+' the Free Software Foundation; either version 2 of the License, or
+' (at your option) any later version.
+'
+' This program is distributed in the hope that it will be useful,
+' but WITHOUT ANY WARRANTY; without even the implied warranty of
+' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+' GNU General Public License for more details.
+'
+' You should have received a copy of the GNU General Public License
+' along with this program; if not, write to the Free Software
+' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+'
+' -------------------------------------------------------------------------
+'
+' $Id$
+'
+SuperStrict
+Import noddybox.mwidget
+Import noddybox.algorithm
+Import brl.audio
+Import brl.audiosample
+
+Const MAGIC:String="SFXV0001"
+
+Type TPoint
+ Field x:Int
+ Field y:Int
+
+ Function Create:TPoint(x:Int, y:Int)
+ Local o:TPoint=New TPoint
+ o.x=x
+ o.y=y
+ Return o
+ End Function
+
+ Method Draw(drag:Int)
+ If drag
+ SetColor(255,255,0)
+ Else
+ SetColor(220,220,220)
+ EndIf
+ DrawRect(x-2,y-2,5,5)
+ End Method
+
+ Method InRange:Int(mx:Int, my:Int)
+ Return Abs(mx-x)<3 And Abs(my-y)<3
+ End Method
+
+ Method ToStream(s:TStream)
+ WriteInt(s,x)
+ WriteInt(s,y)
+ End Method
+
+ Function FromStream:TPoint(s:TStream)
+ Local x:Int=ReadInt(s)
+ Local y:Int=ReadInt(s)
+ Return Create(x,y)
+ End Function
+End Type
+
+Type TPointList
+ Field arr:Object[]
+ Field link:TLink
+ Field x:Int
+ Field p:Int
+ Field cur:TList
+ Field depth:Int
+
+ Function Create:TPointList(l:TList, depth:Int)
+ Local o:TPointList=New TPointList
+ o.arr=l.ToArray()
+ o.depth=depth-1
+ o.Reset()
+ Return o
+ End Function
+
+ Method NextLine()
+ p:+1
+ If p=arr.length
+ Return
+ EndIf
+ Local p1:TPoint=TPoint(arr[p-1])
+ Local p2:TPoint=TPoint(arr[p])
+ cur=DoLine(p1.x,p1.y,p2.x,p2.y)
+ End Method
+
+ Method AtEnd:Int()
+ Return p=arr.length
+ End Method
+
+ Method Point:Double()
+ Local p:TAlgoPoint=TAlgoPoint(cur.RemoveFirst())
+
+ If Not cur.Count()
+ NextLine()
+ EndIf
+
+ Return Double(depth-p.y)/depth
+ End Method
+
+ Method Reset()
+ x=0
+ p=0
+ NextLine()
+ End Method
+End Type
+
+Type EnvelopeControl Extends TMCanvas
+ Field points:TList
+ Field drag:TPoint
+ Field pleft:TPoint
+ Field pright:TPoint
+ Field mx:Int
+ Field my:Int
+
+ Method New()
+ points=CreateList()
+ drag=Null
+ pleft=Null
+ pright=Null
+ End Method
+
+ Function Init:TMCanvas(x:Int, y:Int, w:Int, h:Int, p:TMWidget)
+ Local o:TMCanvas=New EnvelopeControl.Create(x,y,w,h,p)
+ Return o
+ End Function
+
+ Method Clear()
+ points.Clear()
+ points.AddLast(TPoint.Create(0,Height()/2))
+ points.AddLast(TPoint.Create(Width()-1,Height()/2))
+ End Method
+
+ Method ToStream(s:TStream)
+ WriteInt(s,points.Count())
+ For Local p:TPoint=EachIn points
+ p.ToStream(s)
+ Next
+ End Method
+
+ Method FromStream(s:TStream)
+ Local c:Int=ReadInt(s)
+ points.Clear()
+ For Local f:Int=1 To c
+ points.AddLast(TPoint.FromStream(s))
+ Next
+ Draw()
+ End Method
+
+ Method LocatePoint()
+ pleft=Null
+ Local l:TLink=points.FirstLink()
+
+ While l
+ Local p:TPoint=TPoint(l.Value())
+
+ If p.InRange(mx,my)
+ drag=p
+ l=l.NextLink()
+ If l
+ pright=TPoint(l.Value())
+ EndIf
+ Return
+ End If
+
+ If p.x>mx
+ drag=TPoint.Create(mx,my)
+ pright=p
+ points.InsertBeforeLink(drag,l)
+ Return
+ EndIf
+
+ pleft=p
+ l=l.NextLink()
+ Wend
+
+ drag=Null
+ pleft=Null
+ pright=Null
+ End Method
+
+ Method Draw()
+ SetupGraphics()
+
+ Cls()
+
+ Local prev:TPoint=Null
+
+ SetColor(180,180,180)
+
+ For Local p:TPoint=EachIn points
+ If prev
+ DrawLine(prev.x,prev.y,p.x,p.y)
+ EndIf
+ prev=p
+ Next
+
+ For Local p:TPoint=EachIn points
+ p.Draw(p=drag)
+ Next
+
+ Flip
+ End Method
+
+ Method OnRedraw:Int()
+ Draw()
+ End Method
+
+ Method OnMouseEnter:Int()
+ SetPointer(POINTER_CROSS)
+ End Method
+
+ Method OnMouseLeave:Int()
+ SetPointer(POINTER_DEFAULT)
+ End Method
+
+ Method OnButtonDown:Int(b:Int)
+ If Not drag
+ LocatePoint()
+ Draw()
+ EndIf
+ End Method
+
+ Method OnButtonUp:Int(b:Int)
+ If drag
+ If pleft And pleft.InRange(drag.x,drag.y)
+ points.Remove(drag)
+ ElseIf pright And pright.InRange(drag.x,drag.y)
+ points.Remove(drag)
+ End If
+ drag=Null
+ pleft=Null
+ pright=Null
+ Draw()
+ EndIf
+ End Method
+
+ Method OnMouseMove:Int(x:Int, y:Int)
+ mx=x
+ my=y
+
+ If drag
+ If pleft And pright
+ drag.x=Max(pleft.x,Min(pright.x,mx))
+ ElseIf pleft
+ drag.x=Width()-1
+ Else
+ drag.x=0
+ EndIf
+
+ drag.y=Max(0,Min(Height()-1,my))
+
+ Draw()
+ EndIf
+ End Method
+
+ Method OnMouseWheel:Int(b:Int)
+ End Method
+
+ Method OnKeyDown:Int(b:Int)
+ End Method
+
+ Method OnKeyUp:Int(b:Int)
+ End Method
+
+ Method OnKey:Int(b:Int)
+ End Method
+
+ Method GetPointList:TPointList()
+ Return TPointList.Create(points,Height())
+ End Method
+End Type
+
+Type WaveControl Extends EnvelopeControl
+ Const EMPTY:Int=0
+ Const SINE:Int=1
+ Const SQUARE:Int=2
+ Const SAW:Int=3
+ Const TRIANGLE:Int=4
+ Const NOISE:Int=5
+
+ Method SetWave(t:Int)
+ Select t
+ Case EMPTY
+ Clear()
+
+ Case SINE
+ points.Clear()
+ For Local x:Int=0 Until Width()
+ points.AddLast(TPoint.Create(x,Height()/2+Sin(x)*((Height()-5)/2)))
+ Next
+
+
+ Case SQUARE
+ points.Clear()
+ points.AddLast(TPoint.Create(0,0))
+ points.AddLast(TPoint.Create(Width()/2,0))
+ points.AddLast(TPoint.Create(Width()/2,Height()-1))
+ points.AddLast(TPoint.Create(Width()-1,Height()-1))
+
+
+ Case SAW
+ points.Clear()
+ points.AddLast(TPoint.Create(0,Height()-1))
+ points.AddLast(TPoint.Create(Width()-1,0))
+
+ Case TRIANGLE
+ points.Clear()
+ points.AddLast(TPoint.Create(0,Height()-1))
+ points.AddLast(TPoint.Create(Width()/2,0))
+ points.AddLast(TPoint.Create(Width()-1,Height()-1))
+
+ Case NOISE
+ points.Clear()
+ For Local x:Int=0 Until Width()
+ points.AddLast(TPoint.Create(x,Rand(0,Height()-1)))
+ Next
+
+ Default
+ Clear()
+ End Select
+
+ Draw()
+ End Method
+End Type
+
+Type SampleControl Extends TMCanvas
+ Field sample:TAudioSample
+ Field sound:TSound
+
+ Method Draw()
+ SetupGraphics()
+ Cls
+ If sample
+ Local s:Int=Max(1,sample.length/Width())
+ Local p:Int=0
+ Local x:Int=0
+
+ SetColor(255,255,255)
+
+ While p<sample.length
+ 'Plot(x,Height()-1-sample.samples[p])
+ DrawLine(x,Height()-1,x,Height()-1-sample.samples[p]/3)
+ p:+s
+ x:+1
+ Wend
+ EndIf
+ Flip
+ End Method
+
+ Method OnRedraw:Int()
+ Draw()
+ End Method
+
+ Method Generate(env:TPointList, wave:TPointList)
+ Const PERSTEP:Int=100
+ Const PERWAV:Int=1
+ Const WAVSPD:Int=10
+ sample=CreateAudioSample(400*PERSTEP,44100,SF_MONO8)
+
+ Local p:Int=0
+ Local ep:Double
+ Local wp:Double
+ Local wd:Int=PERWAV
+
+ ep=env.Point()
+ wp=wave.Point()
+ While Not env.AtEnd() And p<400*PERSTEP
+ For Local f:Int=0 Until PERSTEP
+ If Not wd
+ wd=PERWAV
+ For Local f:Int=0 Until WAVSPD
+ If wave.AtEnd()
+ wave.Reset()
+ EndIf
+
+ wp=wave.Point()
+ Next
+ Else
+ wd:-1
+ EndIf
+
+ sample.samples[p]=(ep*wp)*255
+ p:+1
+ Next
+ ep=env.Point()
+ Wend
+ Draw()
+ End Method
+
+ Method Play()
+ sound:TSound=LoadSound(sample,False)
+ PlaySound(sound)
+ End Method
+
+ Method Clear()
+ sample=Null
+ sound=Null
+ Draw()
+ End Method
+End Type
+
|