@@ -19,17 +19,17 @@ export class SoundService {
19
19
this . playbackSource = new AudioBufferSourceNode ( this . context ) ;
20
20
}
21
21
22
- async playPause ( ) : Promise < void > {
22
+ async playPause ( ) : Promise < void > {
23
23
this . isPlaying = ! this . isPlaying ;
24
24
if ( this . isPlaying ) {
25
25
const loopBuffer = await this . getRenderedBuffer ( ) ;
26
26
this . playSound ( loopBuffer ) ;
27
- } else {
27
+ } else {
28
28
this . pause ( ) ;
29
29
}
30
30
}
31
31
32
- pause ( ) {
32
+ pause ( ) {
33
33
this . playbackSource . stop ( this . context . currentTime ) ;
34
34
this . reset ( ) ;
35
35
}
@@ -39,12 +39,14 @@ export class SoundService {
39
39
source . buffer = loopBuffer ;
40
40
source . connect ( this . context . destination ) ;
41
41
source . loop = true ;
42
+ source . loopStart = source . buffer . duration / 2 ;
43
+ source . loopEnd = source . buffer . duration ;
42
44
const startTime = this . context . currentTime ;
43
- source . start ( this . context . currentTime ) ;
45
+ source . start ( ) ;
44
46
45
47
const updateDisplay = ( ) => {
46
48
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 ) ;
48
50
if ( this . isPlaying )
49
51
requestAnimationFrame ( updateDisplay ) ;
50
52
} ;
@@ -65,11 +67,15 @@ export class SoundService {
65
67
return quaterBeat ;
66
68
}
67
69
70
+ //Rendered sound (twice the grid length) ████░░░░
71
+ //Played sound (the doted part is looped) ████░░░░░░░░░░░░░░░░░░░░░░░░
72
+ //Used to avoid sound clip ;)
68
73
async getRenderedBuffer ( ) {
69
74
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 ) ;
71
76
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 ) => {
73
79
if ( ! beat )
74
80
return ;
75
81
@@ -90,6 +96,12 @@ export class SoundService {
90
96
}
91
97
92
98
99
+ private getDuplicatedTrackSteps ( track : Track ) {
100
+ let duplicatedTracks = [ ...track . steps ]
101
+ duplicatedTracks . push ( ...track . steps ) ;
102
+ return duplicatedTracks ;
103
+ }
104
+
93
105
reset ( ) : void {
94
106
this . isPlaying = false ;
95
107
this . index = 0 ;
@@ -109,8 +121,10 @@ export class SoundService {
109
121
trackNames . forEach ( x => this . samples . push ( new Sample ( x ) ) )
110
122
for ( const sample of this . samples ) {
111
123
this . getAudioBuffer ( sample . fileName ) . then ( arrayBuffer => sample . sample = arrayBuffer )
112
- . then ( ( ) => { } )
113
- . catch ( ( ) => { } ) ;
124
+ . then ( ( ) => {
125
+ } )
126
+ . catch ( ( ) => {
127
+ } ) ;
114
128
}
115
129
}
116
130
0 commit comments