Skip to content

Commit 25cd2f8

Browse files
author
Baptiste LYET
committed
Stop clipping
1 parent c0b874e commit 25cd2f8

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/app/services/sound.service.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ export class SoundService {
1919
this.playbackSource = new AudioBufferSourceNode(this.context);
2020
}
2121

22-
async playPause() : Promise<void> {
22+
async playPause(): Promise<void> {
2323
this.isPlaying = !this.isPlaying;
2424
if (this.isPlaying) {
2525
const loopBuffer = await this.getRenderedBuffer();
2626
this.playSound(loopBuffer);
27-
}else {
27+
} else {
2828
this.pause();
2929
}
3030
}
3131

32-
pause(){
32+
pause() {
3333
this.playbackSource.stop(this.context.currentTime);
3434
this.reset();
3535
}
@@ -39,12 +39,14 @@ export class SoundService {
3939
source.buffer = loopBuffer;
4040
source.connect(this.context.destination);
4141
source.loop = true;
42+
source.loopStart = source.buffer.duration / 2;
43+
source.loopEnd = source.buffer.duration;
4244
const startTime = this.context.currentTime;
43-
source.start(this.context.currentTime);
45+
source.start();
4446

4547
const updateDisplay = () => {
4648
const currentTime = this.context.currentTime - startTime;
47-
this.index = Math.trunc(((currentTime*1000)/this.getMillisStepFromBpm())%16);
49+
this.index = Math.trunc(((currentTime * 1000) / this.getMillisStepFromBpm()) % 16);
4850
if (this.isPlaying)
4951
requestAnimationFrame(updateDisplay);
5052
};
@@ -65,11 +67,15 @@ export class SoundService {
6567
return quaterBeat;
6668
}
6769

70+
//Rendered sound (twice the grid length) ████░░░░
71+
//Played sound (the doted part is looped) ████░░░░░░░░░░░░░░░░░░░░░░░░
72+
//Used to avoid sound clip ;)
6873
async getRenderedBuffer() {
6974
const tickLength = this.getTickLength();
70-
const offlineContext: OfflineAudioContext = new OfflineAudioContext(1, 16 * tickLength * 44100, 44100);
75+
const offlineContext: OfflineAudioContext = new OfflineAudioContext(1, 16 * 2 * tickLength * 44100, 44100);
7176
this.tracks.forEach((track: Track) => {
72-
track.steps.forEach((beat: boolean, i: number) => {
77+
let trackSteps = this.getDuplicatedTrackSteps(track);
78+
trackSteps.forEach((beat: boolean, i: number) => {
7379
if (!beat)
7480
return;
7581

@@ -90,6 +96,12 @@ export class SoundService {
9096
}
9197

9298

99+
private getDuplicatedTrackSteps(track: Track) {
100+
let duplicatedTracks = [...track.steps]
101+
duplicatedTracks.push(...track.steps);
102+
return duplicatedTracks;
103+
}
104+
93105
reset(): void {
94106
this.isPlaying = false;
95107
this.index = 0;
@@ -109,8 +121,10 @@ export class SoundService {
109121
trackNames.forEach(x => this.samples.push(new Sample(x)))
110122
for (const sample of this.samples) {
111123
this.getAudioBuffer(sample.fileName).then(arrayBuffer => sample.sample = arrayBuffer)
112-
.then(() => {})
113-
.catch(() => {});
124+
.then(() => {
125+
})
126+
.catch(() => {
127+
});
114128
}
115129
}
116130

0 commit comments

Comments
 (0)