From 0cda8a7783697986605d09d0edbcd1a4e6b3f9f9 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Mon, 11 Oct 2021 17:15:16 -0400 Subject: [PATCH] add Wave section with sine/square/triangle/sawtooth generator --- Makefile | 2 +- src/application.jsx | 60 ++++++++++++++++++++++++++++--- src/sine_wave_data.jsx | 26 -------------- src/wave_data.jsx | 82 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 31 deletions(-) delete mode 100644 src/sine_wave_data.jsx create mode 100644 src/wave_data.jsx diff --git a/Makefile b/Makefile index a13f75b..21ec242 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ all: js/application.js js/preact.min.js | js -js/application.js : src/main.jsx src/application.jsx src/note_input.jsx src/sine_wave_data.jsx +js/application.js : src/main.jsx src/application.jsx src/note_input.jsx src/wave_data.jsx esbuild --bundle --jsx-factory=preact.h --jsx-fragment=preact.Fragment --format=esm \ src/main.jsx --outfile=js/application.js diff --git a/src/application.jsx b/src/application.jsx index f77c3c1..584cfb6 100644 --- a/src/application.jsx +++ b/src/application.jsx @@ -3,7 +3,7 @@ import { NoteInput, NoteFrequency } from './note_input'; import { RadioGroup } from './radio_group'; -import { SineWaveData } from './sine_wave_data'; +import { WaveData } from './wave_data'; function calc_sr(osc) { // iigs is ~7.14Mhz / 8. Mirage is 8Mhz / 8 @@ -67,6 +67,26 @@ function Frequency(props) { } + +function Assembler(props) { + + var options = ["Merlin", "ORCA/M"].map( (o, ix) => { + return ; + }); + + return ; +} + +function WaveShape(props) { + + var options = ["Sine", "Square", "Triangle", "Sawtooth"].map( (o, ix) => { + return ; + }); + + return ; +} + + function nmultiply(x) { if (x == 0) return 0; if (x == 1) return n; @@ -148,7 +168,6 @@ function NoteDisplay(props) {
Wave Size: 256
Resolution: {best_res}
Frequency: {Math.round(best_freq)}
- ); @@ -172,8 +191,10 @@ export class Application extends preact.Component { this._freqChange = this.freqChange.bind(this); this._noteChange = this.noteChange.bind(this); this._tabChange = this.tabChange.bind(this); + this._asmChange = this.asmChange.bind(this); + this._shapeChange = this.shapeChange.bind(this); - this.state = { osc: 32, wave: 0, res: 0, freq: 512, tab: 0, note: 4*12 }; + this.state = { osc: 32, wave: 0, res: 0, freq: 512, tab: 0, note: 4*12, assembler: 0, shape: 0 }; } oscChange(e) { @@ -212,6 +233,18 @@ export class Application extends preact.Component { this.setState({ note: v }); } + asmChange(e) { + e.preventDefault(); + var v = +e.target.value; + this.setState({ assembler: v}); + } + + shapeChange(e) { + e.preventDefault(); + var v = +e.target.value; + this.setState({ shape: v}); + } + sampleChildren() { var { osc, wave, res, freq } = this.state; @@ -257,6 +290,24 @@ export class Application extends preact.Component { } + waveChildren() { + + var { assembler, shape } = this.state; + return ( + <> +
+ +
+ +
+ +
+ + + + ); + } + render() { @@ -268,9 +319,10 @@ export class Application extends preact.Component { switch(tab){ case 0: children = this.sampleChildren(); break; case 1: children = this.noteChildren(); break; + case 2: children = this.waveChildren(); break; } - var options = ["Sample", "Note"].map( (o, ix) => { + var options = ["Sample", "Note", "Wave"].map( (o, ix) => { return ; }); diff --git a/src/sine_wave_data.jsx b/src/sine_wave_data.jsx deleted file mode 100644 index 0316fb9..0000000 --- a/src/sine_wave_data.jsx +++ /dev/null @@ -1,26 +0,0 @@ - - -export function SineWaveData() { - - var data = []; - for (var n = 0; n < 256; ++n) { - var x = 128 + Math.round(127 * Math.sin(n * Math.PI / 128)); - data.push( x || 1 ); - } - - var hex = data.map( (x) => x < 0x10 ? "0" + x.toString(16) : x.toString(16) ); - - var code = []; - for (var n = 0; n < 16; ++n) { - var line = "\t hex " + hex.slice(n * 16, n * 16 + 16).join("") + "\n" - code.push(line); - } - - return ( - -
-		{code}
-		
-
- ); -} \ No newline at end of file diff --git a/src/wave_data.jsx b/src/wave_data.jsx new file mode 100644 index 0000000..99e45cd --- /dev/null +++ b/src/wave_data.jsx @@ -0,0 +1,82 @@ + + + + +function sine() { + + var rv = []; + for (var n = 0; n < 256; ++n) { + var x = 128 + Math.round(127 * Math.sin(n * Math.PI / 128)); + rv.push( x || 1 ); + } + return rv; + +} + +function square() { + + var rv = []; + for (var n = 0; n < 128; ++n) rv.push(255); + for (var n = 0; n < 128; ++n) rv.push(1); + return rv; +} + +function triangle() { + // 0x80 -> 0xff [25%] -> 0x80 [50%] -> 0x01 [75%] -> 0x80 [100%] + var rv = []; + for (var n = 0; n < 64; ++n) rv.push(0x80 + n * 2); + for (var n = 0; n < 128; ++n) rv.push(0xff - n * 2); + for (var n = 0; n < 64; ++n) rv.push(0x01 + n * 2); + return rv; +} + + +function sawtooth() { + + var rv = []; + for (var n = 0; n < 128; ++n) rv.push(0x80 + n); + for (var n = 0; n < 128; ++n) rv.push(0x01 + n); + return rv; +} + + +export function WaveData(props) { + + + var {assembler, shape} = props; + + var data; + switch(shape) { + case 0: data = sine(); break; + case 1: data = square(); break; + case 2: data = triangle(); break; + case 3: data = sawtooth(); break; + } + + var hex = data.map( (x) => x < 0x10 ? "0" + x.toString(16) : x.toString(16) ); + + var code = []; + if (assembler == 0) { + // merlin + for (var n = 0; n < 16; ++n) { + var line = " hex " + hex.slice(n * 16, n * 16 + 16).join("") + "\n" + code.push(line); + } + } + + if (assembler == 1) { + // orca/m + for (var n = 0; n < 16; ++n) { + var line = " dc h'" + hex.slice(n * 16, n * 16 + 16).join("") + "'\n" + code.push(line); + } + } + + return ( + +
+		{code}
+		
+
+ ); +}