Browse code

Starting work on audio example

neauoire authored on 02/04/2021 18:50:37
Showing 4 changed files
... ...
@@ -11,6 +11,7 @@
11 11
 |0140 ;Keys { key 1 }
12 12
 |0150 ;Mouse { x 2 y 2 state 1 chord 1 }
13 13
 |0160 ;File { pad 8 name 2 length 2 load 2 save 2 }
14
+|0170 ;Audio { ch1asdr 2 ch2asdr 2 ch3asdr 2 ch4asdr 2 ch1pitch 1 ch1vol 1 ch2pitch 1 ch2vol 1 ch3pitch 1 ch3vol 1 ch4pitch 1 ch4vol 1 }
14 15
 |01F0 ;System { pad 8 r 2 g 2 b 2 }
15 16
 
16 17
 ( vectors )
17 18
new file mode 100644
... ...
@@ -0,0 +1,191 @@
1
+( a blank file )
2
+
3
+%RTN { JMP2r }
4
+%8+ { #0008 ADD2 } %8- { #0008 SUB2 }
5
+%MOD { DUP2 DIV MUL SUB }
6
+
7
+( variables )
8
+
9
+;pointer { x 2 y 2 }
10
+;color { byte 1 }
11
+
12
+( devices )
13
+
14
+|0100 ;Console { pad 8 char 1 byte 1 short 2 string 2 }
15
+|0110 ;Screen { width 2 height 2 pad 4 x 2 y 2 color 1 }
16
+|0120 ;Sprite { pad 8 x 2 y 2 addr 2 color 1 }
17
+|0130 ;Controller { p1 1 }
18
+|0140 ;Keys { key 1 }
19
+|0150 ;Mouse { x 2 y 2 state 1 chord 1 }
20
+|0160 ;File { pad 8 name 2 length 2 load 2 save 2 }
21
+|01F0 ;System { pad 8 r 2 g 2 b 2 }
22
+
23
+( vectors )
24
+
25
+|0200 ^RESET JMP
26
+|0204 ,ERROR JMP2
27
+|0208 ,FRAME JMP2
28
+
29
+@RESET
30
+
31
+	( theme ) #f0fd =System.r #f0f3 =System.g #f0f2 =System.b
32
+
33
+	,draw-timeline JSR2
34
+
35
+BRK
36
+
37
+@FRAME
38
+
39
+	,draw-cursor JSR2
40
+
41
+BRK
42
+
43
+@ERROR
44
+
45
+BRK
46
+
47
+@draw-bars ( -- )
48
+	
49
+
50
+	#0040 =Sprite.y
51
+	,font_hex =Sprite.addr
52
+	#0000 #0100 
53
+	$loop 
54
+		OVR2 SWP POP #02 DIV #0f AND #00 NEQ ^$skip JNZ
55
+			OVR2 #0060 ADD2 =Sprite.x
56
+			#01 =Sprite.color
57
+			~Sprite.addr 8+ =Sprite.addr
58
+			OVR2 #0060 ADD2 #0051 #00c1 #01 ,line-vertical-dotted JSR2
59
+		$skip
60
+		OVR2 #0060 ADD2 #0050 #00c0 #01 ,line-vertical-dotted JSR2
61
+		SWP2 #0008 ADD2 SWP2
62
+		OVR2 OVR2 LTH2 ^$loop JNZ
63
+	POP2
64
+	POP2
65
+
66
+RTN
67
+
68
+@draw-octave ( x y -- )
69
+
70
+	=Sprite.y
71
+	=Sprite.x
72
+	,octave_icn =Sprite.addr
73
+
74
+	~Sprite.y ~Sprite.y #0038 ADD2
75
+	$loop
76
+		OVR2 =Sprite.y
77
+		#01 =Sprite.color
78
+		~Sprite.addr 8+ =Sprite.addr
79
+		~Sprite.x 8+ =Sprite.x
80
+		#01 =Sprite.color
81
+		~Sprite.addr 8+ =Sprite.addr
82
+		~Sprite.x 8- =Sprite.x
83
+		SWP2 #0008 ADD2 SWP2
84
+		OVR2 OVR2 LTH2 ^$loop JNZ
85
+	POP2
86
+	POP2
87
+
88
+
89
+RTN
90
+
91
+@draw-octaves ( -- )
92
+	
93
+	#0040 #0050 ,draw-octave JSR2
94
+	#0040 #0088 ,draw-octave JSR2
95
+
96
+	#0028 =Sprite.x
97
+	#0080 =Sprite.y
98
+	,font_hex #0060 ADD2 =Sprite.addr
99
+	#01 =Sprite.color 
100
+	#0030 =Sprite.x
101
+	,font_hex #0018 ADD2 =Sprite.addr
102
+	#01 =Sprite.color 
103
+
104
+	#0028 =Sprite.x
105
+	#00b8 =Sprite.y
106
+	,font_hex #0060 ADD2 =Sprite.addr
107
+	#01 =Sprite.color 
108
+	#0030 =Sprite.x
109
+	,font_hex #0010 ADD2 =Sprite.addr
110
+	#01 =Sprite.color 
111
+
112
+RTN
113
+
114
+@draw-timeline ( -- )
115
+
116
+	,draw-bars JSR2
117
+	,draw-octaves JSR2
118
+
119
+RTN
120
+
121
+@line-vertical-dotted ( x y0 y1 color -- )
122
+	
123
+	=color STH2 SWP2 =Screen.x STH2r OVR2 =Screen.y
124
+	$draw-ver
125
+		( draw ) ~color =Screen.color
126
+		( incr ) SWP2 #0002 ADD2 DUP2 =Screen.y SWP2
127
+		OVR2 OVR2 LTH2 ^$draw-ver JNZ
128
+	POP2 POP2
129
+
130
+RTN
131
+
132
+@draw-cursor ( -- )
133
+
134
+	( clear last cursor )
135
+	,clear_icn =Sprite.addr 
136
+	~pointer.x =Sprite.x 
137
+	~pointer.y =Sprite.y 
138
+	#10 =Sprite.color
139
+
140
+	( record pointer positions )
141
+	~Mouse.x =pointer.x ~Mouse.y =pointer.y
142
+
143
+	( draw new cursor )
144
+	,cursor_icn =Sprite.addr 
145
+	~pointer.x =Sprite.x 
146
+	~pointer.y =Sprite.y 
147
+	#11 ~Mouse.state #00 NEQ ADD =Sprite.color
148
+
149
+RTN
150
+
151
+@clear_icn   [ 0000 0000 0000 0000 ]
152
+@cursor_icn  [ 80c0 e0f0 f8e0 1000 ]
153
+
154
+@piano-white [ 
155
+	ffc0 8080 80c0 ff00
156
+	fc02 0202 0202 fc00
157
+]
158
+
159
+@piano-black [ 
160
+	ffff fefe feff ff00 
161
+	f008 0808 0808 f000
162
+]
163
+
164
+@octave_icn [
165
+	ff00 0000 fefe fffe 
166
+	ff01 0101 0102 fc00 
167
+	fffe fc00 fefe fffe 
168
+	ff01 0101 0102 fc00 
169
+	fffe fc00 fefe fffe 
170
+	ff01 0101 0102 fc00 
171
+	fffe fc00 0000 ff00 
172
+	ff01 0101 0102 fc00 
173
+	ff00 0000 fefe fffe 
174
+	ff01 0101 0102 fc00 
175
+	fffe fc00 fefe fffe 
176
+	ff01 0101 0102 fc00 
177
+	fffe fc00 0000 ff00 
178
+	ff01 0101 0102 fc00 
179
+]
180
+
181
+@font_hex ( 0-F ) 
182
+[
183
+	007c 8282 8282 827c 0030 1010 1010 1010
184
+	007c 8202 7c80 80fe 007c 8202 1c02 827c
185
+	000c 1424 4484 fe04 00fe 8080 7c02 827c
186
+	007c 8280 fc82 827c 007c 8202 1e02 0202
187
+	007c 8282 7c82 827c 007c 8282 7e02 827c
188
+	007c 8202 7e82 827e 00fc 8282 fc82 82fc 
189
+	007c 8280 8080 827c 00fc 8282 8282 82fc 
190
+	007c 8280 f080 827c 007c 8280 f080 8080
191
+]
... ...
@@ -42,7 +42,7 @@ BRK
42 42
 
43 43
 BRK 
44 44
 
45
-@draw-cursor
45
+@draw-cursor ( -- )
46 46
 
47 47
 	( clear last cursor )
48 48
 	,clear_icn =Sprite.addr 
... ...
@@ -51,19 +51,20 @@ Uint8 font[][8] = {
51 51
 
52 52
 #define SAMPLE_FREQUENCY 48000
53 53
 
54
-static Uint32 note_periods[12] = { /* middle C (C4) is note 60 */
55
-	(Uint32) 0xfa7e * SAMPLE_FREQUENCY, /* C-1 */
56
-	(Uint32) 0xec6f * SAMPLE_FREQUENCY,
57
-	(Uint32) 0xdf2a * SAMPLE_FREQUENCY, /* D-1 */
58
-	(Uint32) 0xd2a4 * SAMPLE_FREQUENCY,
59
-	(Uint32) 0xc6d1 * SAMPLE_FREQUENCY, /* E-1 */
60
-	(Uint32) 0xbba8 * SAMPLE_FREQUENCY, /* F-1 */
61
-	(Uint32) 0xb120 * SAMPLE_FREQUENCY,
62
-	(Uint32) 0xa72f * SAMPLE_FREQUENCY, /* G-1 */
63
-	(Uint32) 0x9dcd * SAMPLE_FREQUENCY,
64
-	(Uint32) 0x94f2 * SAMPLE_FREQUENCY, /* A-1 */
65
-	(Uint32) 0x8c95 * SAMPLE_FREQUENCY,
66
-	(Uint32) 0x84b2 * SAMPLE_FREQUENCY  /* B-1 */
54
+static Uint32 note_periods[12] = {
55
+	/* middle C (C4) is note 60 */
56
+	(Uint32)0xfa7e * SAMPLE_FREQUENCY, /* C-1 */
57
+	(Uint32)0xec6f * SAMPLE_FREQUENCY,
58
+	(Uint32)0xdf2a * SAMPLE_FREQUENCY, /* D-1 */
59
+	(Uint32)0xd2a4 * SAMPLE_FREQUENCY,
60
+	(Uint32)0xc6d1 * SAMPLE_FREQUENCY, /* E-1 */
61
+	(Uint32)0xbba8 * SAMPLE_FREQUENCY, /* F-1 */
62
+	(Uint32)0xb120 * SAMPLE_FREQUENCY,
63
+	(Uint32)0xa72f * SAMPLE_FREQUENCY, /* G-1 */
64
+	(Uint32)0x9dcd * SAMPLE_FREQUENCY,
65
+	(Uint32)0x94f2 * SAMPLE_FREQUENCY, /* A-1 */
66
+	(Uint32)0x8c95 * SAMPLE_FREQUENCY,
67
+	(Uint32)0x84b2 * SAMPLE_FREQUENCY /* B-1 */
67 68
 };
68 69
 
69 70
 static struct audio_channel {
... ...
@@ -237,25 +238,26 @@ togglezoom(Uxn *u)
237 238
 }
238 239
 
239 240
 void
240
-audio_callback(void* userdata, Uint8* stream, int len) {
241
-	Sint16 *samples = (Sint16 *) stream;
241
+audio_callback(void *userdata, Uint8 *stream, int len)
242
+{
243
+	Sint16 *samples = (Sint16 *)stream;
242 244
 	int i, j;
243 245
 	len >>= 1; /* use len for number of samples, not bytes */
244
-	for (j = 0; j < len; ++j) samples[j] = 0;
245
-	for (i = 0; i < 4; ++i) {
246
+	for(j = 0; j < len; ++j) samples[j] = 0;
247
+	for(i = 0; i < 4; ++i) {
246 248
 		struct audio_channel *c = &channels[i];
247
-		if (!c->volume) continue;
248
-		if (c->period < (1 << 20)) continue;
249
-		for (j = 0; j < len; ++j) {
249
+		if(!c->volume) continue;
250
+		if(c->period < (1 << 20)) continue;
251
+		for(j = 0; j < len; ++j) {
250 252
 			c->count += 1 << 20;
251
-			while (c->count > c->period) {
253
+			while(c->count > c->period) {
252 254
 				c->value = !c->value;
253 255
 				c->count -= c->period;
254 256
 			}
255 257
 			samples[j] += (c->value * 2 - 1) * c->volume;
256 258
 		}
257 259
 	}
258
-	(void) userdata;
260
+	(void)userdata;
259 261
 }
260 262
 
261 263
 void
... ...
@@ -462,7 +464,7 @@ Uint8
462 464
 audio_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
463 465
 {
464 466
 	Uint8 *m = u->ram.dat;
465
-	if (b0 & 1) {
467
+	if(b0 & 1) {
466 468
 		Uint16 channel_addr = ptr + (b0 & 0x6);
467 469
 		struct audio_channel *c = &channels[(b0 & 0x6) >> 1];
468 470
 		SDL_LockAudioDevice(audio_id);