summaryrefslogtreecommitdiff
path: root/types.bmx
blob: 26924cfc86d7f58c7c687f07a24de6ce4e314648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
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: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