Skip to content

Commit

Permalink
The Comment Update
Browse files Browse the repository at this point in the history
every .js file is now commented with a short description
  • Loading branch information
spessasus committed May 20, 2024
1 parent d23aac5 commit 1bad907
Show file tree
Hide file tree
Showing 50 changed files with 283 additions and 32 deletions.
5 changes: 5 additions & 0 deletions src/spessasynth_lib/midi_handler/midi_handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Synthetizer } from '../synthetizer/synthetizer.js'
import { consoleColors } from '../utils/other.js';

/**
* midi_handler.js
* purpose: handles the connection between MIDI devices and synthesizer/sequencer via Web MIDI API
*/

const NO_INPUT = null;

export class MIDIDeviceHandler
Expand Down
7 changes: 7 additions & 0 deletions src/spessasynth_lib/midi_handler/web_midi_link.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Synthetizer } from '../synthetizer/synthetizer.js'
import { consoleColors } from '../utils/other.js'

/**
* web_midi_link.js
* purpose: handles the web midi link connection to the synthesizer
* https://www.g200kg.com/en/docs/webmidilink/
*/

export class WebMidiLinkHandler
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/spessasynth_lib/midi_parser/midi_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {
readVariableLengthQuantity
} from "../utils/byte_functions.js";
import { arrayToHexString, consoleColors, formatTitle } from '../utils/other.js'

/**
* midi_loader.js
* purpose: parses a midi file for the seqyencer, including things like marker or CC 2/4 loop detection, copyright detection etc.
*/
export class MIDI{
/**
* Parses a given midi file
Expand Down
4 changes: 4 additions & 0 deletions src/spessasynth_lib/midi_parser/midi_message.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {ShiftableByteArray} from "../utils/shiftable_array.js";

/**
* midi_message.js
* purpose: contains enums for midi events and controllers and functions to parse them
*/

export class MidiMessage
{
Expand Down
5 changes: 5 additions & 0 deletions src/spessasynth_lib/sequencer/sequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { getEvent, messageTypes, midiControllers, MidiMessage } from '../midi_pa
import { consoleColors, formatTime } from '../utils/other.js'
import {readBytesAsUintBigEndian} from "../utils/byte_functions.js";

/**
* sequencer.js
* purpose: plays back the midi file decoded by midi_loader.js, including support for multi-channel midis (adding channels when more than 1 midi port is detected)
*/

const MIN_NOTE_TIME = 0.02;
const MAX_NOTEONS_PER_S = 200;

Expand Down
5 changes: 5 additions & 0 deletions src/spessasynth_lib/soundfont/chunk/generators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { ShiftableByteArray } from '../../utils/shiftable_array.js'
import { RiffChunk } from './riff_chunk.js'
import { readByte, signedInt16 } from '../../utils/byte_functions.js'

/**
* generators.js
* purpose: contains enums for generators and their limis parses reads soundfont generators, sums them and applies limits
*/

export const generatorTypes = {
startAddrsOffset: 0, // sample control - moves sample start point
endAddrOffset: 1, // sample control - moves sample end point
Expand Down
5 changes: 5 additions & 0 deletions src/spessasynth_lib/soundfont/chunk/instruments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import {RiffChunk} from "./riff_chunk.js";
import {InstrumentZone} from "./zones.js";
import {readBytesAsString, readBytesAsUintLittleEndian} from "../../utils/byte_functions.js";

/**
* instrument.js
* purpose: parses soundfont instrument and stores them as a class
*/

export class Instrument{
/**
* Creates an instrument
Expand Down
4 changes: 4 additions & 0 deletions src/spessasynth_lib/soundfont/chunk/modulators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { generatorTypes } from './generators.js'
import { consoleColors } from '../../utils/other.js'
import { midiControllers } from '../../midi_parser/midi_message.js'

/**
* modulators.js
* purpose: parses soundfont modulators and the source enums, also includes the default modulators list
**/
export const modulatorSources = {
noController: 0,
noteOnVelocity: 2,
Expand Down
4 changes: 4 additions & 0 deletions src/spessasynth_lib/soundfont/chunk/presets.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {Sample} from "./samples.js";
import { Generator, generatorTypes } from './generators.js'
import { defaultModulators } from './modulators.js'

/**
* parses soundfont presets, also includes function for getting the generators and samples from midi note and velocity
*/

export class Preset {
/**
* Creates a preset
Expand Down
31 changes: 30 additions & 1 deletion src/spessasynth_lib/soundfont/chunk/riff_chunk.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import {ShiftableByteArray} from "../../utils/shiftable_array.js";
import { ShiftableByteArray } from '../../utils/shiftable_array.js'
import { readBytesAsString, readBytesAsUintLittleEndian } from '../../utils/byte_functions.js'

/**
* riff_chunk.js
* reads a riff chunk and stores it as a class
*/

export class RiffChunk
{
/**
Expand All @@ -14,4 +21,26 @@ export class RiffChunk
this.chunkData = data;
}

}

/**
* @param dataArray {ShiftableByteArray}
* @param readData {boolean}
* @returns {RiffChunk}
*/
export function readRIFFChunk(dataArray, readData = true) {
let header = readBytesAsString(dataArray, 4)

let size = readBytesAsUintLittleEndian(dataArray, 4)
let chunkData = undefined
if (readData) {
chunkData = new ShiftableByteArray(size)
chunkData.set(dataArray.slice(dataArray.currentIndex, dataArray.currentIndex + size))
dataArray.currentIndex += size
// for (let i = 0; i < size; i++) {
// chunkData[i] = readByte(dataArray);
// }
}

return new RiffChunk(header, size, chunkData)
}
12 changes: 9 additions & 3 deletions src/spessasynth_lib/soundfont/chunk/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ import {RiffChunk} from "./riff_chunk.js";
import {ShiftableByteArray} from "../../utils/shiftable_array.js";
import {readByte, readBytesAsUintLittleEndian, readBytesAsString, signedInt8} from "../../utils/byte_functions.js";
import { consoleColors } from '../../utils/other.js';

/**
* samples.js
* purpose: parses soundfont samples, resamples if needed.
* loads sample data, handles async loading of sf3 compressed samples
*/

const FIX_SAMPLERATE = 44100;

/**
* Reads the generatorTranslator from the shdr chunk
* @param sampleHeadersChunk {RiffChunk}
* @param smplChunkData {ShiftableByteArray}
* @returns {Sample[]}
*/

const FIX_SAMPLERATE = 44100 ;

export function readSamples(sampleHeadersChunk, smplChunkData)
{
/**
Expand Down
7 changes: 6 additions & 1 deletion src/spessasynth_lib/soundfont/chunk/zones.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import {Sample} from "./samples.js";
import {Instrument} from "./instruments.js";
import {Modulator} from "./modulators.js";

/**
* zones.js
* purpose: reads instrumend and preset zones from soundfont and gets their respective samples and generators and modulators
*/

export class InstrumentZone {
/**
* Creates a zone (presetinstrument)
* Creates a zone (instrument)
* @param dataArray {ShiftableByteArray}
*/
constructor(dataArray) {
Expand Down
9 changes: 7 additions & 2 deletions src/spessasynth_lib/soundfont/soundfont_parser.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import {ShiftableByteArray} from "../utils/shiftable_array.js";
import {readSamples} from "./chunk/samples.js";
import { readRIFFChunk, readBytesAsString, readBytesAsUintLittleEndian } from '../utils/byte_functions.js'
import { readBytesAsString, readBytesAsUintLittleEndian } from '../utils/byte_functions.js'
import {readGenerators, Generator} from "./chunk/generators.js";
import {readInstrumentZones, InstrumentZone, readPresetZones} from "./chunk/zones.js";
import {Preset, readPresets} from "./chunk/presets.js";
import {readInstruments, Instrument} from "./chunk/instruments.js";
import {readModulators, Modulator} from "./chunk/modulators.js";
import {RiffChunk} from "./chunk/riff_chunk.js";
import { readRIFFChunk, RiffChunk } from './chunk/riff_chunk.js'
import { consoleColors } from '../utils/other.js'

/**
* soundfont_parser.js
* purpose: parses a soundfont2 file
*/

export class SoundFont2
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/spessasynth_lib/synthetizer/fancy_chorus.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* fancy_chorus.js
* purpose: creates a simple chorus effect node
*/

const DEFAULT_DELAY = 0.02;
const DELAY_VARIATION = 0.015;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {Generator, generatorTypes} from "../../soundfont/chunk/generators.js";
import {Sample} from "../../soundfont/chunk/samples.js";

/**
* generator_translator.js
* purpose: translates the generators got from the preset to values understandable by voice.js
*/

const EMU_ATTENUATION_CORRECTION = 0.4;
const FREQ_MIN = 0;
const FREQ_MAX = 22050;
Expand Down
5 changes: 5 additions & 0 deletions src/spessasynth_lib/synthetizer/native_system/midi_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import {Preset} from "../../soundfont/chunk/presets.js";
import { consoleColors } from '../../utils/other.js'
import { midiControllers } from '../../midi_parser/midi_message.js'

/**
* midi_channel.js
* purpose: manages a single midi channel for the native system
*/

const CHANNEL_LOUDNESS = 0.3;

const dataEntryStates = {
Expand Down
6 changes: 6 additions & 0 deletions src/spessasynth_lib/synthetizer/native_system/voice.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { GeneratorTranslator } from './generator_translator.js';

/**
* voice.js
* purpose: manages a single voice (sample) with the whole synthesis model (oscillator, lowpass filter, gain controller etc.)
*/


/**
* Creates a new instance of a single sample
* @param sampleAndGenerators {SampleAndGenerators}
Expand Down
5 changes: 5 additions & 0 deletions src/spessasynth_lib/synthetizer/native_system/voice_group.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {Preset} from "../../soundfont/chunk/presets.js";
import { getVoiceAsync, Voice } from './voice.js'

/**
* voice_group.js
* purpose: manages the group of voices created by a single noteOn message
*/

export class VoiceGroup
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/spessasynth_lib/synthetizer/synthetizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { WorkletChannel } from './worklet_system/worklet_channel.js'
import { EventHandler } from '../utils/synth_event_handler.js'
import { FancyChorus } from './fancy_chorus.js'

/**
* synthesizer.js
* purpose: responds to midi messages and called functions, managing the channels and passing the messages to them
*/

const VOICES_CAP = 450;

export const DEFAULT_GAIN = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import { applyVolumeEnvelope } from './worklet_utilities/volume_envelope.js'
import { applyLowpassFilter } from './worklet_utilities/lowpass_filter.js'
import { getModEnvValue } from './worklet_utilities/modulation_envelope.js'

/**
* channel_processor.js
* purpose: manages the channel from the AudioWorkletGlobalScope and renders the audio data
*/

const CHANNEL_CAP = 400;
const CONTROLLER_TABLE_SIZE = 147;
const MIN_NOTE_LENGTH = 0.07; // if the note is released faster than that, it forced to last that long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { consoleColors } from '../../utils/other.js'
import { modulatorSources } from '../../soundfont/chunk/modulators.js'
import { midiControllers } from '../../midi_parser/midi_message.js'
import { clearSamplesList, getWorkletVoices } from './worklet_utilities/worklet_voice.js'

/**
* worklet_channel.sj
* purpose: manages a single midi channel in the worklet system and communicates with channel_processor.js
*/

const CHANNEL_GAIN = 0.5;

export const NON_CC_INDEX_OFFSET = 128;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* lfo.js
* purpose: low frequency triangel oscillator
*/

/**
* Calculates a triangular wave value for the given time
* @param startTime {number} seconds
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { generatorTypes } from '../../../soundfont/chunk/generators.js'
import { absCentsToHz, decibelAttenuationToGain } from './unit_converter.js'

/**
* lowpass_filter.js
* purpose: applies a low pass filter to a voice
*/


/**
* Applies a low-pass filter to the given buffer
* @param voice {WorkletVoice} the voice we're working on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { generatorTypes } from '../../../soundfont/chunk/generators.js'
import { getModulatorCurveValue } from './modulator_curves.js'
import { modulatorCurveTypes } from '../../../soundfont/chunk/modulators.js'

/**
* modulation_envelope.js
* purpose: calculates the modulation envelope for the given voice
*/
const PEAK = 1;

// 1000 should be precise enough
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { modulatorCurveTypes } from '../../../soundfont/chunk/modulators.js'

/**
* modulator_curves.js
* precomputes modulator concave and conves curves and calculates a curve value for a given polarity, direction and type
*/

// the length of the precomputed curve tables
export const MOD_PRECOMPUTED_LENGTH = 16384;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { HALF_PI } from './unit_converter.js'

/**
* stereo_panner.js
* purpose: pans a given voice out to the stereo output and to the effects' outputs
*/

/**
* Pans the voice to the given output buffers
* @param pan {number} 0-1 , 0.5 is middle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* unit_converter.js
* purpose: converts soundfont units into more useable values with the use of lookup tables to improve performance
*/


// timecent lookup table
const MIN_TIMECENT = -15000;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { decibelAttenuationToGain, timecentsToSeconds } from './unit_converter.js'
import { generatorTypes } from '../../../soundfont/chunk/generators.js'

/**
* volume_envelope.js
* purpose: applies a volume envelope for a given voice
*/

const DB_SILENCE = 100;
const GAIN_SILENCE = 0.005;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* wavetable_oscillator.js
* purpose: plays back raw audio data at an arbitrary playback rate
*/


/**
* Fills the output buffer with raw sample data
* @param voice {WorkletVoice} the voice we're working on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { NON_CC_INDEX_OFFSET } from '../worklet_channel.js'
import { modulatorSources } from '../../../soundfont/chunk/modulators.js'
import { getModulatorCurveValue, MOD_PRECOMPUTED_LENGTH } from './modulator_curves.js'

/**
* worklet_modulator.js
* purpose: precomputes all curve types and computes modulators
*/

/**
* Computes a given modulator
* @param controllerTable {Int16Array} all midi controllers as 14bit values + the non controller indexes, starting at 128
Expand Down
Loading

0 comments on commit 1bad907

Please sign in to comment.