Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/index.ts
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;
16 changes: 16 additions & 0 deletions src/modules/AudioContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ class AudioContextManager {
}
return this.audioContext.createPeriodicWave(real, imag, constraints);
}

/**
* Get current time of audio context
* @return {Number}
*/
public get currentTime(): number {
return this.getAudioContext().currentTime;
}

/**
* Get audio destination node for audio context
* @return {AudioDestinationNode}
*/
public get audioDestination(): AudioDestinationNode {
return this.getAudioContext().destination;
}
}

export default new AudioContextManager();
3 changes: 1 addition & 2 deletions src/modules/filters/BandPassFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default class BandPassFilter extends Filter {
constructor() {
super();
this.node.type = 'bandpass';
this.node.frequency.value = 440;
this.node.Q.value = 1000;
this.node.frequency.value = 1000;
}
}
22 changes: 22 additions & 0 deletions src/modules/noises/BrownianNoise.ts
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;
}
}
}
130 changes: 130 additions & 0 deletions src/modules/noises/Noise.ts
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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это свойство сейчас нигде не используется


/**
* 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 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

может этот код лучше в конструктор перенести? тк тут определяются основные свойства, а приватный метод где-то внизу. Пусть лучше будет в конструкторе наверху

const audioContext = audioContextManager.getAudioContext();

/**
* Configure buffer
* 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);
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();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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;
}
29 changes: 29 additions & 0 deletions src/modules/noises/PinkNoise.ts
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;
}
}
}
16 changes: 16 additions & 0 deletions src/modules/noises/WhiteNoise.ts
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;
}
}
}
38 changes: 38 additions & 0 deletions src/noise-test/ChilloutAudio.ts
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();
}
}
}
52 changes: 52 additions & 0 deletions src/noise-test/Track.ts
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();
}
}