Skip to content

Commit

Permalink
Correction erreurs linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Babali42 committed Jan 18, 2024
1 parent 9f19f1c commit ec32288
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/app/components/sequencer/sequencer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class SequencerComponent implements OnInit {
}

toggleIsPlaying(): void{
this.soundService.playPause();
this.soundService.playPause().then();
}
}

4 changes: 2 additions & 2 deletions src/app/models/primary/jsonBeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export interface JsonBeat {

export class Convert{
public static toBeat(jsonBeat: JsonBeat): Beat{
let tracks = jsonBeat.tracks.map(x => Convert.toTrack(x));
const tracks = jsonBeat.tracks.map(x => Convert.toTrack(x));
return new Beat(jsonBeat.name, jsonBeat.bpm, tracks);
}

private static toTrack(jsonTrack: JsonTrack): Track {
let steps = jsonTrack.steps
const steps = jsonTrack.steps
.map(x => x.trim())
.map(x => Boolean(x));
return new Track(jsonTrack.name, jsonTrack.fileName, steps);
Expand Down
5 changes: 3 additions & 2 deletions src/app/services/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import {JsonBeat} from "../models/primary/jsonBeat";

@Injectable({
providedIn: 'root'
Expand All @@ -12,7 +13,7 @@ export class DataService {

constructor(private http: HttpClient) { }

getData(fileName: string): Observable<any> {
return this.http.get(this.apiUrl + fileName + this.fileExtension);
getData(fileName: string): Observable<JsonBeat> {
return this.http.get<JsonBeat>(this.apiUrl + fileName + this.fileExtension);
}
}
51 changes: 24 additions & 27 deletions src/app/services/sound.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,58 @@ export class SoundService {
private tracks: Track[] = [];
private ms: number = this.getMillisStepFromBpm();
private cursor = 0;
private playbackSource: AudioBufferSourceNode | null = null;
private playbackSource: AudioBufferSourceNode;

constructor() {
this.context = new AudioContext();
this.cursor = 0;
this.playbackSource = new AudioBufferSourceNode(this.context);
}

async playPause() {
this.isPlaying = !this.isPlaying;
if (this.isPlaying) {
var loopBuffer = await this.getRenderedBuffer();
const loopBuffer = await this.getRenderedBuffer();
this.playSound(loopBuffer);
}else {
// @ts-ignore
this.playbackSource.stop(this.context.currentTime);
}
}

private playSound(loopBuffer: AudioBuffer) {
var source = this.context.createBufferSource();
const source = this.context.createBufferSource();
source.buffer = loopBuffer;
source.connect(this.context.destination);
source.loop = true;
source.start(this.context.currentTime, this.cursor * this.getTickLength());
if (this.playbackSource) {
if (this.playbackSource.buffer) {
this.playbackSource.stop(this.context.currentTime);
}
this.playbackSource = source;
}

private getMillisStepFromBpm(): number {
let beat = 60000 / this.bpm;
let quaterBeat = beat / 4;
let beat = 60000 / this.bpm, quaterBeat = beat / 4;
quaterBeat = Math.min(quaterBeat, 1000);
quaterBeat = Math.max(quaterBeat, 10);
return quaterBeat;
}

async getRenderedBuffer() {
var tickLength = this.getTickLength();
var offlineContext = new OfflineAudioContext(1, 16 * tickLength * 44100, 44100);

const tickLength = this.getTickLength();
const offlineContext: OfflineAudioContext = new OfflineAudioContext(1, 16 * tickLength * 44100, 44100);
this.tracks.forEach((track: Track) => {
track.steps.forEach((beat: boolean, i: number) => {
if (beat) {
let audioBuffer = this.samples.find(x => x.fileName === track.fileName)!.sample!;
let audioBufferSourceNode = offlineContext.createBufferSource();
audioBufferSourceNode.buffer = audioBuffer;
audioBufferSourceNode.connect(offlineContext.destination);

var when = i * tickLength;
audioBufferSourceNode.start(when);
}
if (!beat)
return;

let audioBuffer = this.samples.find(x => x.fileName === track.fileName)!.sample!;
let audioBufferSourceNode = offlineContext.createBufferSource();
audioBufferSourceNode.buffer = audioBuffer;
audioBufferSourceNode.connect(offlineContext.destination);

const when = i * tickLength;
audioBufferSourceNode.start(when);
});
});

Expand All @@ -88,21 +87,19 @@ export class SoundService {

setTracks(tracks: Track[]) {
this.tracks = tracks;
let trackNames = tracks.map(x => x.fileName);
const trackNames = tracks.map(x => x.fileName);
this.loadTracks(trackNames);
}

private loadTracks(trackNames: string[]) {
trackNames.forEach(x => this.samples.push(new Sample(x)))
this.samples.forEach((sample) => {
this.getAudioBuffer(sample.fileName).then(arrayBuffer => {
sample.sample = arrayBuffer;
});
});
for (const sample of this.samples) {
this.getAudioBuffer(sample.fileName).then(arrayBuffer => sample.sample = arrayBuffer);
}
}

private async getAudioBuffer(soundName: String): Promise<AudioBuffer> {
let myRequest = new Request(`assets/sounds/${soundName}`);
private async getAudioBuffer(soundName: string): Promise<AudioBuffer> {
const myRequest = new Request(`assets/sounds/${soundName}`);
const response = await fetch(myRequest);
const arrayBuffer = await response.arrayBuffer();
return await this.context.decodeAudioData(arrayBuffer).then((data) => {
Expand Down

0 comments on commit ec32288

Please sign in to comment.