-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into add-audio-playback
- Loading branch information
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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
16 changes: 16 additions & 0 deletions
16
apps/fretonator-web/src/app/common/playback/note-playback.service.spec.ts
This file contains 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 { TestBed } from '@angular/core/testing'; | ||
|
||
import { NotePlaybackService } from './note-playback.service'; | ||
|
||
describe('NotePlaybackService', () => { | ||
let service: NotePlaybackService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(NotePlaybackService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
81 changes: 81 additions & 0 deletions
81
apps/fretonator-web/src/app/common/playback/note-playback.service.ts
This file contains 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,81 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { StringFrequencies } from '../../util/constants'; | ||
|
||
const SYNTH_BUFFER_SIZE = 4096; | ||
const SYNTH_PLAY_DURATION = 2000; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class NotePlaybackService { | ||
private context: AudioContext; | ||
|
||
constructor() {} | ||
|
||
playNote(stringName, fret) { | ||
if (!this.context) { | ||
try { | ||
// Feature sniff for web audio API | ||
this.context = new (window.AudioContext || window['webkitAudioContext']); | ||
} catch (e) { | ||
// No browser support :( | ||
} | ||
} | ||
if(this.context){ | ||
const noteFrequency = this.getFrequency(stringName, fret); | ||
this.pluckString(noteFrequency); | ||
} | ||
} | ||
|
||
private getFrequency(stringName, fret) { | ||
// We're using stringName here, the case sensitive alt to string, to differentiate E/e strings. | ||
const stringFrequency = StringFrequencies[stringName]; | ||
const fretCents = fret * 100; | ||
return stringFrequency * Math.pow(2, (fretCents / 1200)); | ||
} | ||
|
||
private pluckString(frequency: number) { | ||
// Use Karplus-Strong algo to simply synth guitar-like sounds. | ||
// https://ccrma.stanford.edu/~jos/pasp/Karplus_Strong_Algorithm.html | ||
const processor = this.context.createScriptProcessor(SYNTH_BUFFER_SIZE, 0, 1); | ||
const signalPeriod = Math.round(this.context.sampleRate / frequency); | ||
const currentSample = new Float32Array(signalPeriod); | ||
// Fill sample with random noise -1 through +1 | ||
this.fillWithNoise(currentSample, signalPeriod); | ||
let n = 0; | ||
processor.addEventListener('audioprocess', (e) => { | ||
// Populate output buffer with signal | ||
const outputBuffer = e.outputBuffer.getChannelData(0); | ||
for (let i = 0; i < outputBuffer.length; i++) { | ||
// Lowpass the signal by averaging it with the next point | ||
currentSample[n] = (currentSample[n] + currentSample[(n + 1) % signalPeriod]) / 2; | ||
// Copy output to the buffer, repeat | ||
outputBuffer[i] = currentSample[n]; | ||
n = (n + 1) % signalPeriod; | ||
} | ||
}); | ||
// Filter the output | ||
const bandpass = this.createBandpassFilter(frequency); | ||
processor.connect(bandpass); | ||
// Kill the processor after 2 seconds | ||
setTimeout(() => { | ||
bandpass.disconnect(); | ||
processor.disconnect(); | ||
}, SYNTH_PLAY_DURATION); | ||
} | ||
|
||
private fillWithNoise(sample, signalPeriod){ | ||
for (let i = 0; i < signalPeriod; i++) { | ||
sample[i] = (2 * Math.random()) - 1; | ||
} | ||
} | ||
|
||
private createBandpassFilter(frequency){ | ||
const bandpass = this.context.createBiquadFilter(); | ||
bandpass.type = "bandpass"; | ||
bandpass.frequency.value = Math.round(frequency); | ||
bandpass.Q.value = 1 / 6; | ||
bandpass.connect(this.context.destination); | ||
return bandpass; | ||
} | ||
} |
This file contains 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