javascript code description - Mr-Kumar-Abhishek/brain-beats GitHub Wiki
JavaScript Code Description
This document describes the JavaScript code files in the Brain Beats web application.
js/main.js
1. This file contains the main application logic for sound generation and user interaction.
Functions:
start_dreamachine(freq)
: Initiates the Dreamachine effect by adding the "flicker" class to the body and cards, and sets the animation duration based on the input frequencyfreq
. It toggles between starting and stopping the Dreamachine effect.stop_dreamachine()
: Stops the Dreamachine effect by removing the "flicker" class from the body and cards.start_dreamachine_generator()
: Retrieves the frequency value from the input field with ID "freq" and callsstart_dreamachine()
to start the Dreamachine effect with that frequency.volume_set()
: Retrieves the volume level from the input field with ID "volume", calculates the program volume (dividing by 100), and returns it.play_solfeggio(freq)
: Starts playing Solfeggio frequencies by stopping all other sounds, setting thesolfeggio_flag
to 1, and callingplay_single_tone()
with the given frequency. It toggles between starting and stopping Solfeggio sounds.stop_solfeggio()
: Stops playing Solfeggio frequencies by settingsolfeggio_flag
to 0 and callingstop_single_tone()
.play_pure_tone(freq)
: Starts playing a pure tone by stopping all other sounds, setting thepure_tone_flag
to 1, and callingplay_single_tone()
with the given frequency. It toggles between starting and stopping pure tones.stop_pure_tone()
: Stops playing pure tones by settingpure_tone_flag
to 0 and callingstop_single_tone()
.play_angel(freq)
: Starts playing Angel frequencies by stopping all other sounds, setting theangel_flag
to 1, and callingplay_single_tone()
with the given frequency. It toggles between starting and stopping Angel frequencies.stop_angel()
: Stops playing Angel frequencies by settingangel_flag
to 0 and callingstop_single_tone()
.adjustFrequency(frequency)
: Adjusts the inputfrequency
to be within the human hearing range (20Hz - 20kHz) by increasing or decreasing octaves as needed.play_single_tone(freq, oscillator_type)
: Plays a single tone using the Web Audio API OscillatorNode. It takes a frequencyfreq
and oscillator typeoscillator_type
as arguments, creates an oscillator, connects it to the audio context destination through a volume gain node, and starts the oscillator. It toggles between starting and stopping single tones.play_kundalini(beat_freq_1)
: Starts playing Kundalini Ascension frequencies by stopping all other sounds, settingkundalini_flag
to 1, calculatingbeat_freq_2
, and callingplay_monaural()
withbeat_freq_1
andbeat_freq_2
. It toggles between starting and stopping Kundalini sounds.play_monaural(freq1, freq2)
: Starts playing monaural beats by stopping all other sounds, settingmonaural_flag
to 1, and callingplay_double_tone()
with frequenciesfreq1
,freq2
, waveform 'sine', and deviation type 'monaural'. It toggles between starting and stopping monaural beats.play_sq_monaural(freq1, freq2)
: Starts playing square wave monaural beats by stopping all other sounds, settingsq_monaural_flag
to 1, and callingplay_double_tone()
with frequenciesfreq1
,freq2
, waveform 'square', and deviation type 'monaural'. It toggles between starting and stopping square wave monaural beats.play_binaural(freq1, freq2)
: Starts playing binaural beats by stopping all other sounds, settingbinaural_flag
to 1, and callingplay_double_tone()
with frequenciesfreq1
,freq2
, waveform 'sine', and deviation type 'binaural'. It toggles between starting and stopping binaural beats.play_double_tone(freq1, freq2, form, deviation)
: Plays dual-tone sounds (both binaural and monaural beats) using two oscillators and panner nodes for stereo separation (for binaural beats). It takes two frequenciesfreq1
,freq2
, waveformform
, and deviation typedeviation
as arguments, creates two oscillators and panner nodes, connects them to the audio context destination through a volume gain node, and starts the oscillators. It toggles between starting and stopping dual-tone sounds.play_isochronic(freq1, freq2)
: Starts playing isochronic tones by stopping all other sounds, settingisochronic_flag
to 1, calculatingbeat_freq_2
, callingplay_single_tone()
withbeat_freq_1
and waveform 'square', and starting an interval timer (toggler
) to toggle volume at the beat frequency. It toggles between starting and stopping isochronic tones.kundalini_rotator()
: An asynchronous function that continuously plays Kundalini Ascension frequencies in a loop, incrementing through thek_arr
frequency array every second, until the yin-yang symbol's state is 0.play_white_noise()
: Starts playing white noise by stopping all other sounds, settingboolWhite
to 1, creating an AudioContext and a WhiteNoiseProcessor Worklet node, connecting it to the audio context destination through a gain node, and connecting and starting the nodes. It toggles between starting and stopping white noise.play_pink_noise()
: Starts playing pink noise, similar toplay_white_noise()
but using PinkNoiseProcessor.play_brown_noise()
: Starts playing brown noise, similar toplay_white_noise()
but using BrownNoiseProcessor.play_red_noise()
: Starts playing red noise, similar toplay_white_noise()
but using RedNoiseProcessor.play_black_noise()
: Starts playing black noise, similar toplay_white_noise()
but using BlackNoiseProcessor.play_blue_noise()
: Starts playing blue noise, similar toplay_white_noise()
but using BlueNoiseProcessor.play_violet_noise()
: Starts playing violet noise, similar toplay_white_noise()
but using VioletNoiseProcessor.play_grey_noise()
: Starts playing grey noise, similar toplay_white_noise()
but using GreyNoiseProcessor and adds a high-shelf filter to shape the noise.play_velvet_noise()
: Starts playing velvet noise, similar toplay_white_noise()
but using VelvetNoiseProcessor.play_green_noise()
: Starts playing green noise, similar toplay_white_noise()
but using GreenNoiseProcessor and adds a low-pass filter to shape the noise.play_orange_noise()
: Starts playing orange noise, similar toplay_white_noise()
but using OrangeNoiseProcessor and adds a low-shelf filter to shape the noise.play_yellow_noise()
: Starts playing yellow noise, similar toplay_white_noise()
but using YellowNoiseProcessor and adds a low-pass filter with Q value adjustment to shape the noise.play_turquoise_noise()
: Starts playing turquoise noise, similar toplay_white_noise()
but using TurquoiseNoiseProcessor and adds a high-pass filter with Q value adjustment to shape the noise.play_rife_monaural_generator()
: Callsplay_sine_monaural_generator()
.play_sine_monaural_generator()
: Retrieves frequency values from input fields with IDs starting with "freq-", stores them intone_freq_array
, and callsplay_sine_monaural()
with the array.play_rife_3d_auto_generator()
: Callsplay_sine_3d_auto_generator()
.play_sine_3d_auto_generator()
: Retrieves frequency values from input fields with IDs starting with "freq-", stores them intone_freq_array
, and callsplay_sine_3d_auto()
with the array.random(min, max)
: Generates a random number betweenmin
andmax
.distributePoints(n)
: Distributesn
points evenly in 3D space using spherical coordinates and returns a 2D array containing x, y, and z coordinates.play_ALT_3d_auto(tone_freq_array)
: Starts playing 3D ALT frequencies automatically, toggling between start and stop states.play_rife_3d_auto(tone_freq_array)
: Starts playing 3D Rife frequencies automatically, toggling between start and stop states.play_sine_3d_auto(tone_freq_array)
: Plays sine wave 3D auto frequencies, handling single frequency binaural beats or multi-frequency 3D sounds based on the length oftone_freq_array
.play_ALT_monaural(tone_freq_array)
: Starts playing monaural ALT frequencies, toggling between start and stop states.play_rife_monaural(tone_freq_array)
: Starts playing monaural Rife frequencies, toggling between start and stop states.play_sine_monaural(tone_freq_array)
: Plays sine wave monaural frequencies, handling single frequency monaural beats or multi-frequency monaural sounds based on the length oftone_freq_array
.play_rife_3d_generator()
: Callsplay_sine_3d_generator()
.play_sine_3d_generator()
: Retrieves frequency and coordinate values from input fields and callsplay_sine_3d()
to play 3D sine wave sounds.play_rife_3d(tone_freq_array, x_values, y_values, z_values)
: Starts playing 3D Rife frequencies, toggling between start and stop states.play_sine_3d(tone_freq_array, x_values, y_values, z_values)
: Plays sine wave 3D sounds using Web Audio API OscillatorNode and PannerNode for spatial positioning. It takes frequency arraytone_freq_array
and coordinate arraysx_values
,y_values
,z_values
as arguments, creates oscillators and panners for each frequency, connects them to the audio context destination through a volume gain node, and starts the oscillators. It toggles between starting and stopping 3D sine wave sounds.play_mind_machine_binaural(freq1, freq2)
: Starts playing mind machine binaural beats by stopping all other sounds, settingbool_mind_machine_binaural
to 1, calculating the beat frequency, callingplay_binaural()
withfreq1
andfreq2
, and callingstart_dreamachine()
with the beat frequency. It toggles between starting and stopping mind machine binaural beats.stop_double_tone()
: Stops dual-tone oscillators and setsdouble_tone_flag
to 0.stop_single_tone()
: Stops the single tone oscillator and setssingle_tone_flag
to 0.stop_monaural()
: Stops monaural beats by callingstop_double_tone()
and setsmonaural_flag
to 0.stop_binaural()
: Stops binaural beats by callingstop_double_tone()
and setsbinaural_flag
to 0.stop_sq_monaural()
: Stops square wave monaural beats by callingstop_double_tone()
and setssq_monaural_flag
to 0.stop_mind_machine_binaural()
: Stops mind machine binaural beats by callingstop_binaural()
,stop_dreamachine()
, and setsbool_mind_machine_binaural
to 0.stop_kundalini()
: Stops Kundalini Ascension frequencies by callingstop_monaural()
and setskundalini_flag
to 0.kundalini_halter()
: Halts the Kundalini Ascension rotator by setting the yin-yang symbol's state to 0, stopping Kundalini sounds, and resettingk_indexer
.kundalini_toggler()
: Toggles the Kundalini Ascension rotator between start and stop states, updating the yin-yang symbol's state and callingkundalini_rotator()
orstop_kundalini()
accordingly.stop_white_noise()
: Stops white noise playback and disconnects the white noise gain node.stop_pink_noise()
: Stops pink noise playback and disconnects the pink noise gain node.stop_brown_noise()
: Stops brown noise playback and disconnects the brown noise gain node.stop_red_noise()
: Stops red noise playback and disconnects the red noise gain node.stop_black_noise()
: Stops black noise playback and disconnects the black noise gain node.stop_green_noise()
: Stops green noise playback and disconnects the green noise gain node.stop_blue_noise()
: Stops blue noise playback and disconnects the blue noise gain node.stop_violet_noise()
: Stops violet noise playback and disconnects the violet noise gain node.stop_grey_noise()
: Stops grey noise playback and disconnects the grey noise gain node.stop_velvet_noise()
: Stops velvet noise playback and disconnects the velvet noise gain node.stop_orange_noise()
: Stops orange noise playback and disconnects the orange noise gain node.stop_yellow_noise()
: Stops yellow noise playback and disconnects the yellow noise gain node.stop_turquoise_noise()
: Stops turquoise noise playback and disconnects the turquoise noise gain node.stop_isochronic()
: Stops isochronic tones by clearing the interval timer (toggler
) and callingstop_single_tone()
.stop_sine()
: Stops all sine wave oscillators in thesine_oscillators
array.stop_ALT_3d_auto()
: Stops 3D ALT auto sounds by settingboolALT3dauto
to 0 and callingstop_sine_3d_auto()
.stop_ALT_monaural()
: Stops monaural ALT sounds by settingboolALTMonaural
to 0 and callingstop_sine_monaural()
.stop_rife_3d()
: Stops 3D Rife sounds by settingboolRife3D
to 0 and callingstop_sine_3d()
.stop_sine_3d()
: Stops 3D sine wave sounds by callingstop_sine()
and settingboolSine3D
to 0.stop_rife_monaural()
: Stops monaural Rife sounds by settingboolRifeMonaural
to 0 and callingstop_sine_monaural()
.stop_sine_monaural()
: Stops sine wave monaural sounds by callingstop_sine()
,stop_monaural()
, and settingboolSineMonaural
to 0.stop_rife_3d_auto()
: Stops 3D Rife auto sounds by settingboolRife3Dauto
to 0 and callingstop_sine_3d_auto()
.stop_sine_3d_auto()
: Stops 3D sine wave auto sounds by callingstop_sine()
,stop_binaural()
, and settingboolSine3Dauto
to 0.play_monaural_generator()
: Retrieves frequency values from input fields with IDs "freq1" and "freq2" and callsplay_monaural()
with these frequencies.play_binaural_generator()
: Retrieves frequency values from input fields with IDs "freq1" and "freq2" and callsplay_binaural()
with these frequencies.play_isochronic_generator()
: Retrieves frequency values from input fields with IDs "freq1" and "freq2" and callsplay_isochronic()
with these frequencies.play_pure_tone_generator()
: Retrieves frequency value from input field with ID "freq" and callsplay_pure_tone()
with this frequency.play_single_tone_generator()
: Retrieves frequency value from input field with ID "freq" and callsplay_pure_tone()
with this frequency.play_sq_monaural_generator()
: Retrieves frequency values from input fields with IDs "freq1" and "freq2" and callsplay_sq_monaural()
with these frequencies.play_mind_machine_binaural_generator()
: Retrieves frequency values from input fields with IDs "freq1" and "freq2" and callsplay_mind_machine_binaural()
with these frequencies.toggle_volume()
: Toggles the volume of isochronic tones between the user-set volume and 0, creating a pulsing effect.live_volume_set()
: Updates the volume of currently playing sounds based on the value of the volume slider (input field with ID "volume"). It checks flags to determine which sound type is active and updates the gain value of the corresponding gain node.stop_all()
: Stops all sound generators by calling individual stop functions for each sound type and noise color, and resets relevant flags.warning(whichy)
: Displays different alert messages based on thewhichy
parameter, providing instructions or disclaimers to the user.disclaimer()
: Shows the instruction modal on page load.
Global Variables:
audioCtx
: Web Audio API context.double_tone_oscillator_1
,double_tone_oscillator_2
,single_tone_oscillator
,sine_oscillators
: Oscillator nodes for different sound generators.*_flag
variables: Flags to track the active state of different sound generators (e.g.,monaural_flag
,binaural_flag
,pure_tone_flag
,white_noise_flag
).volume
: Gain node for volume control.freq
,beat_freq_1
,beat_freq_2
,single_tone_freq
,tone_freq_array
: Variables to store frequency values.bufferSize
: Buffer size for audio processing.*_noise_node
,*_noise_volume
,*_noise_filter
variables: Variables for noise generator nodes, gain nodes, and filter nodes for different noise colors (e.g.,white_noise_node
,white_noise_volume
,greyNoiseFilter
).bool*
variables: Boolean flags to track active noise colors (e.g.,boolWhite
,boolPink
,boolBrown
).bool_*_3dauto
variables: Boolean flags to track active 3D auto sound types (e.g.boolSine3Dauto
,boolRife3Dauto
,boolALT3dauto
).bool_*_monaural
variables: Boolean flags to track active monaural sound types (e.g.boolSineMonaural
,boolRifeMonaural
,boolALTMonaural
).bool_mind_machine_binaural
: Boolean flag to track mind machine binaural beat state.x_value
,y_values
,z_values
: Variables to store 3D spatial coordinates.k_arr
: Array containing frequencies for Kundalini Ascension preset.kundalini_flag
: Flag to track Kundalini Ascension state.k_indexer
: Indexer for Kundalini Ascension frequency array.notification
: Variable for notifications (not fully utilized in provided code).toggler
: Variable for interval timer used in isochronic tones.freq_count
: Global variable to track the number of frequency fields.oscillator_type
: Global variable to store oscillator type, default 'sine'.deviation_type
: Global variable to store deviation type, default 'binaural'.panning_model
: Global variable to store panning model, default 'HRTF'.body
: DOM element for the body.cards
: DOM NodeList for card elements.volume_box
: DOM element for volume control box.duration
: Variable to store animation duration for Dreamachine effect.eventer
: DOM element for event listener (data container).
js/data.js
2. This file handles fetching and displaying preset data, and managing user favorites.
Global Variables:
favorites
: An array to store IDs of favorite presets, retrieved from local storage or initialized as an empty array.dataCards
: DOM element representing the template for data cards.dataContainer
: DOM element to hold data cards.searchInput
: DOM element for the search input field.dPresets
: An array to store data presets, populated from JSON data and used for filtering.
Event Listeners:
- Search Input Listener: Filters data cards based on user input in the search field. It listens for 'input' events on the
searchInput
element. For each input change, it iterates through thedPresets
array and toggles the "d-none" class on each data card element based on whether the card's title or description includes the search input value. - Click Event Listener on
eventer
(dataContainer): Manages favorite presets. It listens for 'click' events on thedataContainer
element. When a click occurs on an element with an ID within thedataContainer
:- It retrieves the ID of the clicked item.
- It checks if the ID is already in the
favorites
array. - If not a favorite, it adds the ID to the
favorites
array and adds the "faved" class to the item. - If already a favorite, it removes the ID from the
favorites
array and removes the "faved" class from the item. - It updates the
favorites
array in local storage.
Functions:
- Fetch and Display Presets: Fetches JSON data from
jsonData
(variable not defined in this file, assumed to be defined elsewhere), then maps each data item to a card element, populates the card with data from the JSON, adds a "faved" class if the preset is in favorites, and appends the card to thedataContainer
. It returns an array of objects (dPresets
) containing data for filtering.
jQuery Event Handlers:
- Search Form Submit Handler: Prevents the default form submission behavior for the search form with ID "search-form".
js/favs.js
3. This file handles adding and removing favorites using local storage.
Global Variables:
eventer
: DOM element for event listener (data container), same as injs/data.js
.
Event Listeners:
- Click Event Listener on
eventer
(dataContainer): Manages favorite presets. (Same as described injs/data.js
).
js/custom-editors.js
4. This file defines custom editor functions, specifically for raw and time inputs, for use with the bootstrap-input-spinner library.
Objects:
customEditors
: An object containing custom editor functions.RawEditor
: A function constructor for a raw editor that does not parse or format input values.parse(customFormat)
: Returns thecustomFormat
as is (no parsing).render(number)
: Returns thenumber
as is (raw rendering).
TimeEditor
: A function constructor for a time editor that parses and renders time values in "hours:minutes" format.parse(customFormat)
: Parses a time string in "hours:minutes" or "minutes" format into total minutes (integer). It handles negative times and trims whitespace.render(number)
: Renders a number (representing minutes) into a time string in "hours:minutes" format, handling negative times and padding minutes with leading zeros.
js/plugins.js
5. This file contains a plugin to prevent console errors in browsers that lack a console object.
Immediately Invoked Function Expression (IIFE):
- Anonymous Function: An IIFE that checks for the existence of the
window.console
object and stubs its methods if it's not available, preventing JavaScript errors in older browsers. It defines an array of console method names and iterates through them, assigning an empty function (noop
) to any undefined console methods.
js/serviceLoader.js
6. This file registers a service worker for offline caching.
Immediately Invoked Function Expression (IIFE):
- Anonymous Function: An IIFE that registers the service worker script (
/sw.js
) when the page loads, if service workers are supported by the browser. It listens for the 'load' event on the window and, if service workers are supported, registers/sw.js
and logs "Service Worker Registered" to the console upon successful registration.
sw.js
7. This file is the service worker script that handles caching and serving website assets for offline functionality, using the Workbox library.
Global Scope:
importScripts('/js/workbox/workbox-v6.5.3/workbox-sw.js')
: Imports the Workbox service worker library.workbox.setConfig({...})
: Configures Workbox to use a specific module path prefix for Workbox files.- Service Worker Logic (inside
if (workbox)
):- Logs "Yay! Workbox is loaded !" to the console if Workbox is successfully loaded.
workbox.precaching.precacheAndRoute([])
: Precaches and routes an empty array of assets (likely populated dynamically in a build process, but empty in the provided code).- Image Caching Route: Registers a route to cache image files (png, gif, jpg, svg) under the "images" cache using a CacheFirst strategy with an ExpirationPlugin to limit cache size and duration.
- Assets Caching Route: Registers a route to cache JS, CSS, SCSS, and JSON files under the "assets" cache using a StaleWhileRevalidate strategy for fast loading with background updates.
- Google Fonts Caching Route: Registers a route to cache Google Fonts files under the "google-fonts" cache using a CacheFirst strategy with a CacheableResponsePlugin to ensure only successful responses are cached.
self.skipWaiting()
: Forces the service worker to activate immediately upon installation.workbox.core.clientsClaim()
: Makes the service worker take control of all clients as soon as it activates.
- Error Logging (inside
else
): Logs "Oops! Workbox didn't load" to the console if Workbox is not loaded.
This document provides a technical overview of the JavaScript code files. For more detailed information, please refer to the code comments within each file.