Cache samples, fix frequency display

This commit is contained in:
Jeremy Penner 2024-11-28 20:02:11 -05:00
parent a36eb82dda
commit 47cd770a9e
4 changed files with 29 additions and 30 deletions

View file

@ -28,7 +28,7 @@ Sample.prototype = {
return copy; return copy;
}, },
get frequency() { get frequency() {
return this.sampleRate / this.increment; return Math.round(this.sampleRate / this.increment);
}, },
set frequency(toneHz) { set frequency(toneHz) {
this.increment = this.sampleRate / toneHz; this.increment = this.sampleRate / toneHz;

BIN
samples/QUACK.WAV Normal file

Binary file not shown.

View file

@ -1,6 +1,7 @@
<script> <script>
import { loadSample } from './lib/sampload.js' import { loadSample } from './lib/sampload.js'
import NES from './lib/NES.svelte' import NES from './lib/NES.svelte'
import SampleTweak from './lib/SampleTweak.svelte';
let rom = $state(null); let rom = $state(null);
let opts = $state({}); let opts = $state({});
@ -24,16 +25,6 @@
}; };
req.send(); req.send();
} }
async function meow() {
const sample = await loadSample("samples/meow1.wav", 700, 10);
opts = { sampleSquare1: sample, sampleSquare2: sample.clone(), sampleTriangle: sample.clone() };
}
async function yeargh() {
const yeargh = await loadSample("samples/YEARGH.WAV", 1300, 1);
opts = { sampleSquare1: yeargh, sampleSquare2: yeargh.clone(), sampleTriangle: yeargh.clone() };
}
meow();
$effect(() => { $effect(() => {
if (romfile && romfile.length > 0) { if (romfile && romfile.length > 0) {
let reader = new FileReader(); let reader = new FileReader();
@ -51,9 +42,7 @@
<hr/> <hr/>
<p>DPad: Arrow keys<br/>B button: Z, A button: X, Select: Tab / C, Start: Return / V</p> <p>DPad: Arrow keys<br/>B button: Z, A button: X, Select: Tab / C, Start: Return / V</p>
<hr/> <hr/>
<button onclick={() => { opts = {} }}>Normal</button> <SampleTweak bind:opts />
<button onclick={meow}>Meow</button>
<button onclick={yeargh}>Yeargh</button>
</main> </main>
<style> <style>

View file

@ -1,10 +1,11 @@
<script> <script>
import { untrack } from 'svelte';
import { loadSample, decodeSample } from './sampload.js' import { loadSample, decodeSample } from './sampload.js'
let { opts = $bindable() } = $props(); let { opts = $bindable({}) } = $props();
let samplefile = $state(); let samplefile = $state();
let frequency = $state(880) let samples = $state([{name: "Normal"}]);
let gain = $state(1) let selectedSample = $state(0)
let frequency = $state(880);
let gain = $state(1);
$effect(() => { $effect(() => {
if (opts.sampleSquare1) { if (opts.sampleSquare1) {
@ -16,8 +17,8 @@
opts.sampleSquare1.gain = gain; opts.sampleSquare1.gain = gain;
} }
}); });
$inspect(frequency) $effect(() => {
function setSample(sample) { let {sample} = samples[selectedSample];
if (sample) { if (sample) {
frequency = sample.frequency; frequency = sample.frequency;
gain = sample.gain; gain = sample.gain;
@ -25,18 +26,22 @@
} else { } else {
opts = {}; opts = {};
} }
});
function addSample(sample, name) {
samples.push({sample, name});
selectedSample = samples.length - 1;
} }
async function meow() { async function loadDefaultSamples() {
setSample(await loadSample("samples/meow1.wav", 720, 10)); samples.push({sample: await loadSample("samples/meow1.wav", 720, 10), name: "Meow"});
selectedSample = samples.length - 1;
samples.push({sample: await loadSample("samples/YEARGH.WAV", 1300, 1), name: "Yeargh"});
samples.push({sample: await loadSample("samples/QUACK.WAV", 700, 1), name: "Quack"});
} }
async function yeargh() { loadDefaultSamples();
setSample(await loadSample("samples/YEARGH.WAV", 1300, 1));
}
meow();
$effect(() => { $effect(() => {
if (samplefile && samplefile.length > 0) { if (samplefile && samplefile.length > 0) {
let reader = new FileReader(); let reader = new FileReader();
reader.onload = async () => { setSample(await decodeSample(reader.result, 880, 1)); } reader.onload = async () => { addSample(await decodeSample(reader.result, 880, 1), samplefile[0].name); }
reader.readAsArrayBuffer(samplefile[0]); reader.readAsArrayBuffer(samplefile[0]);
} }
}); });
@ -44,9 +49,12 @@
<main> <main>
<div> <div>
<button onclick={() => { opts = {} }}>Normal</button> {#each samples as sample, index}
<button onclick={meow}>Meow</button> <button onclick={() => { selectedSample = index; }}
<button onclick={yeargh}>Yeargh</button> style={index == selectedSample ? "border: 2px solid black" : ""}>
{sample.name}
</button>
{/each}
<input bind:files={samplefile} type="file" /> <input bind:files={samplefile} type="file" />
</div> </div>
<div> <div>
@ -55,6 +63,8 @@
<input type="range" bind:value={frequency} min="220" max="2200" step="10"/> <input type="range" bind:value={frequency} min="220" max="2200" step="10"/>
{frequency} {frequency}
</label> </label>
</div>
<div>
<label> <label>
Gain: Gain:
<input type="range" bind:value={gain} min="0.1" max="10" step="0.1" /> <input type="range" bind:value={gain} min="0.1" max="10" step="0.1" />