fix syntax

This commit is contained in:
2026-02-17 09:15:23 -06:00
parent 89bd583e18
commit b466fad95f
2 changed files with 48 additions and 33 deletions

View File

@@ -67,12 +67,12 @@ var voice = {
// Create a new audio player instance
soundwave.create = function(opts) {
opts = opts || {}
var o = opts || {}
var player = {
sample_rate: opts.sample_rate || 44100,
channels: opts.channels || 2,
frames_per_chunk: opts.frames_per_chunk || 1024,
sample_rate: o.sample_rate || 44100,
channels: o.channels || 2,
frames_per_chunk: o.frames_per_chunk || 1024,
voices: [],
pcm_cache: {}
}
@@ -127,8 +127,9 @@ soundwave.create = function(opts) {
decoded = flac.decode(bytes)
}
var normalized = null
if (decoded && decoded.pcm) {
var normalized = normalize_pcm(decoded, path)
normalized = normalize_pcm(decoded, path)
player.pcm_cache[path] = normalized
return normalized
}
@@ -148,6 +149,12 @@ soundwave.create = function(opts) {
var out = Blob()
var frames_written = 0
var frames_available = null
var frames_needed = null
var frames_to_read = null
var start_bit = null
var end_bit = null
var chunk = null
while (frames_written < frames) {
if (pos >= total_frames) {
if (voice.loop) {
@@ -156,14 +163,14 @@ soundwave.create = function(opts) {
break
}
}
var frames_available = total_frames - pos
var frames_needed = frames - frames_written
var frames_to_read = frames_available < frames_needed ? frames_available : frames_needed
var start_bit = pos * bits_per_frame
var end_bit = (pos + frames_to_read) * bits_per_frame
var chunk = source.pcm.read_blob(start_bit, end_bit)
frames_available = total_frames - pos
frames_needed = frames - frames_written
frames_to_read = frames_available < frames_needed ? frames_available : frames_needed
start_bit = pos * bits_per_frame
end_bit = (pos + frames_to_read) * bits_per_frame
chunk = source.pcm.read_blob(start_bit, end_bit)
out.write_blob(chunk)
pos += frames_to_read
@@ -173,9 +180,11 @@ soundwave.create = function(opts) {
voice.pos = pos
// Pad with silence if needed
var silence_frames = null
var silence = null
if (frames_written < frames) {
var silence_frames = frames - frames_written
var silence = dsp.silence(silence_frames, player.channels)
silence_frames = frames - frames_written
silence = dsp.silence(silence_frames, player.channels)
out.write_blob(silence)
}
@@ -186,9 +195,12 @@ soundwave.create = function(opts) {
// Remove finished voices
player.cleanup = function() {
var active = []
for (var i = 0; i < length(player.voices); i++) {
var v = player.voices[i]
var done = v.stopped || (!v.loop && v.pos >= v.source.frames)
var i = 0
var v = null
var done = null
for (i = 0; i < length(player.voices); i++) {
v = player.voices[i]
done = v.stopped || (!v.loop && v.pos >= v.source.frames)
if (!done) {
push(active, v)
} else if (v.finish_hook) {
@@ -223,24 +235,27 @@ soundwave.create = function(opts) {
// Pull mixed audio frames
// Returns a stoned blob of f32 samples (channels * frames * 4 bytes)
player.pull = function(frames) {
frames = frames || player.frames_per_chunk
var nframes = frames || player.frames_per_chunk
var blobs = []
var vols = []
for (var i = 0; i < length(player.voices); i++) {
var v = player.voices[i]
var i = 0
var v = null
var chunk = null
for (i = 0; i < length(player.voices); i++) {
v = player.voices[i]
if (v.stopped) continue
var chunk = pull_voice_chunk(v, frames)
chunk = pull_voice_chunk(v, nframes)
if (chunk) {
push(blobs, chunk)
push(vols, v.vol)
}
}
var mixed
var mixed = null
if (length(blobs) == 0) {
mixed = dsp.silence(frames, player.channels)
mixed = dsp.silence(nframes, player.channels)
} else {
mixed = dsp.mix_blobs(blobs, vols)
}