Skip to content

Commit

Permalink
Ajout animation du curseur (#7)
Browse files Browse the repository at this point in the history
* wip

* Amélioration interface suite

* Clean unused variable

* correction linter
  • Loading branch information
Babali42 authored Feb 28, 2024
1 parent a07c4c1 commit ee54157
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
19 changes: 12 additions & 7 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,26 @@ export class AppComponent implements OnInit {
.subscribe(result => {
this.isMobileDisplay = !result.matches;
});
this.navigate();
}

selectGenre(i: number) {
this.selectedGenreIndex = i;
this.router.navigate([this.musicGenres[this.selectedGenreIndex].subGenres[0].link]).then(
() => {},
() => {},
this.selectedSubGenreIndex = 0;
this.navigate();
}

private navigate() {
this.router.navigate([this.musicGenres[this.selectedGenreIndex].subGenres[this.selectedSubGenreIndex].link]).then(
() => {
},
() => {
},
);
}

selectSubGenre(i: number) {
this.selectedSubGenreIndex = i;
this.router.navigate([this.musicGenres[this.selectedGenreIndex].subGenres[this.selectedSubGenreIndex].link]).then(
() => {},
() => {},
);
this.navigate();
}
}
2 changes: 1 addition & 1 deletion src/app/components/sequencer/sequencer.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</div>
<div class="sequencer-grid">
<div *ngFor="let track of beat.tracks">
<app-track [track]="track" [current-step-index]="0"></app-track>
<app-track [track]="track" [current-step-index]="this.soundService.index"></app-track>
</div>
</div>
</div>
13 changes: 7 additions & 6 deletions src/app/components/sequencer/sequencer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ export class SequencerComponent implements OnInit {
ngOnInit(): void {
this.dataService.getData(this.fileName).subscribe((result: JsonBeat) => {
this.beat = Convert.toBeat(result);
if(this.soundService.isPlaying)
this.soundService.pause();
this.soundService.reset();
this.soundService.setBpm(this.beat.bpm);
this.soundService.setTracks(this.beat.tracks);
});
}

toggleIsPlaying(): void{
this.soundService.playPause().then(() => {})
.catch(error => {
// Handle errors here
console.error('Error:', error);
});
toggleIsPlaying(): void {
this.soundService.playPause().then(
() => {},
() => {}
);
}
}

31 changes: 21 additions & 10 deletions src/app/services/sound.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import {Track} from '../models/track';
export class SoundService {
bpm: number = 120;
isPlaying: boolean = false;

index: number = 0;
private samples: Sample[] = [];
private context: AudioContext;
private tracks: Track[] = [];
private ms: number = this.getMillisStepFromBpm();
private cursor = 0;
private playbackSource: AudioBufferSourceNode;

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

Expand All @@ -28,16 +25,32 @@ export class SoundService {
const loopBuffer = await this.getRenderedBuffer();
this.playSound(loopBuffer);
}else {
this.playbackSource.stop(this.context.currentTime);
this.pause();
}
}

pause(){
this.playbackSource.stop(this.context.currentTime);
this.reset();
}

private playSound(loopBuffer: AudioBuffer) {
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());
const startTime = this.context.currentTime;
source.start(this.context.currentTime);

const updateDisplay = () => {
const currentTime = this.context.currentTime - startTime;
this.index = Math.trunc(((currentTime*1000)/this.getMillisStepFromBpm())%16);
if (this.isPlaying)
requestAnimationFrame(updateDisplay);
};

updateDisplay();

if (this.playbackSource.buffer) {
this.playbackSource.stop(this.context.currentTime);
}
Expand Down Expand Up @@ -79,11 +92,11 @@ export class SoundService {

reset(): void {
this.isPlaying = false;
this.index = 0;
}

setBpm(bpm: number): void {
this.bpm = bpm;
this.ms = this.getMillisStepFromBpm();
}

setTracks(tracks: Track[]) {
Expand All @@ -97,9 +110,7 @@ export class SoundService {
for (const sample of this.samples) {
this.getAudioBuffer(sample.fileName).then(arrayBuffer => sample.sample = arrayBuffer)
.then(() => {})
.catch(error => {
console.error('Error:', error);
});
.catch(() => {});
}
}

Expand Down

0 comments on commit ee54157

Please sign in to comment.