This repository was archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Add noise node #29
Open
ilyamore88
wants to merge
17
commits into
master
Choose a base branch
from
noise-node
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add noise node #29
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b08fb28
add noise node class
ilyamore88 65cb160
add method foe getting current time
ilyamore88 7262692
getter for current time
ilyamore88 9e761af
move public methods up
ilyamore88 ff6cbb0
add docs for filling buffer
ilyamore88 7e0fea7
delete checks
ilyamore88 522517a
add getter for audio destination node
ilyamore88 9217054
fix noise node
ilyamore88 da8e786
add demo
ilyamore88 7d9d5fb
fix bug with discontinuity
ilyamore88 b690ee0
add new noises
ilyamore88 678f619
Merge branches 'master' and 'noise-node' of https://github.com/codex-…
ilyamore88 b253e65
Merge branch 'master' of https://github.com/codex-team/codex.music in…
ilyamore88 9e1485f
refactor noise class
ilyamore88 af8084e
add docs
ilyamore88 0284e74
delete setter for noise destination and set it in connect method
ilyamore88 5e570b0
simplify connect method
ilyamore88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| import ChilloutAudio from './ChilloutAudio'; | ||
| import ChilloutAudio from './noise-test/ChilloutAudio'; | ||
|
|
||
| export default ChilloutAudio; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import Noise from './Noise'; | ||
|
|
||
| /** | ||
| * Class represents brownian noise node | ||
| */ | ||
| export default class BrownianNoise extends Noise { | ||
| /** | ||
| * Fill the buffer with noise | ||
| * @param bufferSize {Number} - size of buffer | ||
| */ | ||
| protected fillBufferData(bufferSize: number): void { | ||
| let lastOut = 0.0; | ||
|
|
||
| for (let i = 0; i < bufferSize; i++) { | ||
| const whiteNoise = Math.random() * 2 - 1; | ||
|
|
||
| this.buffersChannelData[i] = (lastOut + (0.02 * whiteNoise)) / 1.02; | ||
| lastOut = this.buffersChannelData[i]; | ||
| this.buffersChannelData[i] *= 3.5; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import audioContextManager from '../AudioContextManager'; | ||
| import BandPassFilter from '../filters/BandPassFilter'; | ||
|
|
||
| /** | ||
| * Class represents noise node | ||
| */ | ||
| export default abstract class Noise { | ||
| /** | ||
| * Source of noise | ||
| */ | ||
| private bufferSourceNode: AudioBufferSourceNode; | ||
|
|
||
| /** | ||
| * Buffer with data about noise | ||
| */ | ||
| private buffer: AudioBuffer; | ||
|
|
||
| /** | ||
| * Array of frequencies, that creates noise | ||
| */ | ||
| protected buffersChannelData: Float32Array; | ||
|
|
||
| /** | ||
| * Filter that controls noise frequency | ||
| */ | ||
| private bandpass: BandPassFilter; | ||
|
|
||
| /** | ||
| * Current frequency of noise node in hertz | ||
| */ | ||
| private currentFrequency: number = 1000; | ||
|
|
||
| /** | ||
| * Constructor for noise node | ||
| * @param frequency {Number} - noise frequency in hertz | ||
| */ | ||
| constructor(frequency?: number) { | ||
| this.configure(); | ||
| if (frequency) { | ||
| this.setNoiseFrequency(frequency); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Set frequency of noise in hertz | ||
| * @param frequency {Number} - new frequency in hertz | ||
| */ | ||
| public setNoiseFrequency(frequency: number): void { | ||
| this.bandpass.filterNode.frequency.value = frequency; | ||
| this.currentFrequency = frequency; | ||
| } | ||
|
|
||
| /** | ||
| * Method for starting noise node | ||
| */ | ||
| public play(): void { | ||
| this.connect(audioContextManager.audioDestination); | ||
| } | ||
|
|
||
| /** | ||
| * Method for stopping noise node | ||
| */ | ||
| public stop(): void { | ||
| this.disconnect(); | ||
| } | ||
|
|
||
| /** | ||
| * Connect noise node to destination | ||
| * @param destination {AudioNode} - destination for noise node | ||
| */ | ||
| public connect(destination: AudioNode): void { | ||
| this.bandpass.filterNode.connect(destination); | ||
| } | ||
|
|
||
| /** | ||
| * Disconnect noise node from destination | ||
| */ | ||
| private disconnect(): void { | ||
| this.bandpass.filterNode.disconnect(); | ||
| } | ||
|
|
||
| /** | ||
| * Configure noise node | ||
| */ | ||
| private configure(): void { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. может этот код лучше в конструктор перенести? тк тут определяются основные свойства, а приватный метод где-то внизу. Пусть лучше будет в конструкторе наверху |
||
| const audioContext = audioContextManager.getAudioContext(); | ||
|
|
||
| /** | ||
| * Configure buffer | ||
neSpecc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * Buffer has 2x sample rate size for better quality | ||
| */ | ||
| const bufferSize = 2 * audioContext.sampleRate; | ||
|
|
||
| this.buffer = audioContext.createBuffer(1, bufferSize, audioContext.sampleRate); | ||
|
|
||
| /** | ||
| * Get data of the channel 0 | ||
| */ | ||
| this.buffersChannelData = this.buffer.getChannelData(0); | ||
neSpecc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.fillBufferData(bufferSize); | ||
|
|
||
| /** | ||
| * Configure buffer source node | ||
| */ | ||
| this.bufferSourceNode = audioContext.createBufferSource(); | ||
| this.bufferSourceNode.buffer = this.buffer; | ||
|
|
||
| /** | ||
| * The audio asset must be replayed when the end of the AudioBuffer is reached | ||
| */ | ||
| this.bufferSourceNode.loop = true; | ||
|
|
||
| /** | ||
| * Add bandpass filter for filtering required noise frequency | ||
| */ | ||
| this.bandpass = new BandPassFilter(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это можно сразу при объявлении свойства назначить |
||
| this.bufferSourceNode.connect(this.bandpass.filterNode); | ||
|
|
||
| /** | ||
| * Finish configuration | ||
| */ | ||
| this.bufferSourceNode.start(audioContextManager.currentTime); | ||
| } | ||
|
|
||
| /** | ||
| * Fill the buffer with noise | ||
| * @param bufferSize {Number} - size of buffer | ||
| */ | ||
| protected abstract fillBufferData(bufferSize: number): void; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import Noise from './Noise'; | ||
|
|
||
| /** | ||
| * Class represents pink noise node | ||
| */ | ||
| export default class PinkNoise extends Noise { | ||
| /** | ||
| * Fill the buffer with noise | ||
| * @param bufferSize {Number} - size of buffer | ||
| */ | ||
| protected fillBufferData(bufferSize: number): void { | ||
| let b0, b1, b2, b3, b4, b5, b6; | ||
|
|
||
| b0 = b1 = b2 = b3 = b4 = b5 = b6 = 0.0; | ||
| for (let i = 0; i < bufferSize; i++) { | ||
| const whiteNoise = Math.random() * 2 - 1; | ||
|
|
||
| b0 = 0.99886 * b0 + whiteNoise * 0.0555179; | ||
| b1 = 0.99332 * b1 + whiteNoise * 0.0750759; | ||
| b2 = 0.96900 * b2 + whiteNoise * 0.1538520; | ||
| b3 = 0.86650 * b3 + whiteNoise * 0.3104856; | ||
| b4 = 0.55000 * b4 + whiteNoise * 0.5329522; | ||
| b5 = -0.7616 * b5 - whiteNoise * 0.0168980; | ||
| this.buffersChannelData[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + whiteNoise * 0.5362; | ||
| this.buffersChannelData[i] *= 0.11; | ||
| b6 = whiteNoise * 0.115926; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import Noise from './Noise'; | ||
|
|
||
| /** | ||
| * Class represents white noise node | ||
| */ | ||
| export default class WhiteNoise extends Noise { | ||
| /** | ||
| * Fill the buffer with noise | ||
| * @param bufferSize {Number} - size of buffer | ||
| */ | ||
| protected fillBufferData(bufferSize: number): void { | ||
| for (let i = 0; i < bufferSize; i++) { | ||
| this.buffersChannelData[i] = Math.random() * 2 - 1; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import Track from './Track'; | ||
|
|
||
| /** | ||
| * Chillout audio class | ||
| */ | ||
| export default class ChilloutAudio { | ||
| /** | ||
| * Field represents track | ||
| */ | ||
| private track: Track | undefined; | ||
|
|
||
| /** | ||
| * Initialises application | ||
| * @param notes {String} - notes in melody | ||
| * @param instrument {Instruments} - name of instrument | ||
| */ | ||
| public constructor() { | ||
| this.track = new Track(); | ||
| } | ||
|
|
||
| /** | ||
| * Method for start playing melody | ||
| */ | ||
| public play(): void { | ||
| if (this.track) { | ||
| this.track.play(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Method for stop playing melody | ||
| */ | ||
| public stop(): void { | ||
| if (this.track) { | ||
| this.track.stop(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import audioContextManager from '../modules/AudioContextManager'; | ||
| import Noise from '../modules/noises/Noise'; | ||
| import BrownianNoise from '../modules/noises/BrownianNoise'; | ||
| import PinkNoise from '../modules/noises/PinkNoise'; | ||
| import WhiteNoise from '../modules/noises/WhiteNoise'; | ||
|
|
||
| /** | ||
| * Class represents Track implementation and Track configuration | ||
| */ | ||
| export default class Track { | ||
| /** | ||
| * This property implements audio source logic | ||
| */ | ||
| private noise: Noise; | ||
|
|
||
| /** | ||
| * Track status (audio source is already connected with destination) | ||
| */ | ||
| private isConfigured = false; | ||
|
|
||
| /** | ||
| * Constructor for track | ||
| */ | ||
| public constructor() { | ||
| this.noise = new WhiteNoise(100); | ||
| } | ||
|
|
||
| /** | ||
| * Method to connect audio source with destination | ||
| */ | ||
| private configure(): void { | ||
| this.noise.connect(audioContextManager.audioDestination); | ||
| this.isConfigured = true; | ||
| } | ||
|
|
||
| /** | ||
| * Method to play noise | ||
| */ | ||
| public play(): void { | ||
| if (!this.isConfigured) { | ||
| this.configure(); | ||
| } | ||
| this.noise.play(); | ||
| } | ||
|
|
||
| /** | ||
| * Method to stop the track's playback | ||
| */ | ||
| public stop(): void { | ||
| this.noise.stop(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
это свойство сейчас нигде не используется