Skip to content

Commit ee54157

Browse files
authored
Ajout animation du curseur (#7)
* wip * Amélioration interface suite * Clean unused variable * correction linter
1 parent a07c4c1 commit ee54157

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

src/app/app.component.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,26 @@ export class AppComponent implements OnInit {
4444
.subscribe(result => {
4545
this.isMobileDisplay = !result.matches;
4646
});
47+
this.navigate();
4748
}
4849

4950
selectGenre(i: number) {
5051
this.selectedGenreIndex = i;
51-
this.router.navigate([this.musicGenres[this.selectedGenreIndex].subGenres[0].link]).then(
52-
() => {},
53-
() => {},
52+
this.selectedSubGenreIndex = 0;
53+
this.navigate();
54+
}
55+
56+
private navigate() {
57+
this.router.navigate([this.musicGenres[this.selectedGenreIndex].subGenres[this.selectedSubGenreIndex].link]).then(
58+
() => {
59+
},
60+
() => {
61+
},
5462
);
5563
}
5664

5765
selectSubGenre(i: number) {
5866
this.selectedSubGenreIndex = i;
59-
this.router.navigate([this.musicGenres[this.selectedGenreIndex].subGenres[this.selectedSubGenreIndex].link]).then(
60-
() => {},
61-
() => {},
62-
);
67+
this.navigate();
6368
}
6469
}

src/app/components/sequencer/sequencer.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</div>
1010
<div class="sequencer-grid">
1111
<div *ngFor="let track of beat.tracks">
12-
<app-track [track]="track" [current-step-index]="0"></app-track>
12+
<app-track [track]="track" [current-step-index]="this.soundService.index"></app-track>
1313
</div>
1414
</div>
1515
</div>

src/app/components/sequencer/sequencer.component.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ export class SequencerComponent implements OnInit {
2020
ngOnInit(): void {
2121
this.dataService.getData(this.fileName).subscribe((result: JsonBeat) => {
2222
this.beat = Convert.toBeat(result);
23+
if(this.soundService.isPlaying)
24+
this.soundService.pause();
2325
this.soundService.reset();
2426
this.soundService.setBpm(this.beat.bpm);
2527
this.soundService.setTracks(this.beat.tracks);
2628
});
2729
}
2830

29-
toggleIsPlaying(): void{
30-
this.soundService.playPause().then(() => {})
31-
.catch(error => {
32-
// Handle errors here
33-
console.error('Error:', error);
34-
});
31+
toggleIsPlaying(): void {
32+
this.soundService.playPause().then(
33+
() => {},
34+
() => {}
35+
);
3536
}
3637
}
3738

src/app/services/sound.service.ts

+21-10
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ import {Track} from '../models/track';
88
export class SoundService {
99
bpm: number = 120;
1010
isPlaying: boolean = false;
11-
11+
index: number = 0;
1212
private samples: Sample[] = [];
1313
private context: AudioContext;
1414
private tracks: Track[] = [];
15-
private ms: number = this.getMillisStepFromBpm();
16-
private cursor = 0;
1715
private playbackSource: AudioBufferSourceNode;
1816

1917
constructor() {
2018
this.context = new AudioContext();
21-
this.cursor = 0;
2219
this.playbackSource = new AudioBufferSourceNode(this.context);
2320
}
2421

@@ -28,16 +25,32 @@ export class SoundService {
2825
const loopBuffer = await this.getRenderedBuffer();
2926
this.playSound(loopBuffer);
3027
}else {
31-
this.playbackSource.stop(this.context.currentTime);
28+
this.pause();
3229
}
3330
}
3431

32+
pause(){
33+
this.playbackSource.stop(this.context.currentTime);
34+
this.reset();
35+
}
36+
3537
private playSound(loopBuffer: AudioBuffer) {
3638
const source = this.context.createBufferSource();
3739
source.buffer = loopBuffer;
3840
source.connect(this.context.destination);
3941
source.loop = true;
40-
source.start(this.context.currentTime, this.cursor * this.getTickLength());
42+
const startTime = this.context.currentTime;
43+
source.start(this.context.currentTime);
44+
45+
const updateDisplay = () => {
46+
const currentTime = this.context.currentTime - startTime;
47+
this.index = Math.trunc(((currentTime*1000)/this.getMillisStepFromBpm())%16);
48+
if (this.isPlaying)
49+
requestAnimationFrame(updateDisplay);
50+
};
51+
52+
updateDisplay();
53+
4154
if (this.playbackSource.buffer) {
4255
this.playbackSource.stop(this.context.currentTime);
4356
}
@@ -79,11 +92,11 @@ export class SoundService {
7992

8093
reset(): void {
8194
this.isPlaying = false;
95+
this.index = 0;
8296
}
8397

8498
setBpm(bpm: number): void {
8599
this.bpm = bpm;
86-
this.ms = this.getMillisStepFromBpm();
87100
}
88101

89102
setTracks(tracks: Track[]) {
@@ -97,9 +110,7 @@ export class SoundService {
97110
for (const sample of this.samples) {
98111
this.getAudioBuffer(sample.fileName).then(arrayBuffer => sample.sample = arrayBuffer)
99112
.then(() => {})
100-
.catch(error => {
101-
console.error('Error:', error);
102-
});
113+
.catch(() => {});
103114
}
104115
}
105116

0 commit comments

Comments
 (0)