@@ -3,93 +3,96 @@ import p5sound from './master';
3
3
// https://github.com/TONEnoTONE/Tone.js/
4
4
import Clock from 'Tone/core/Clock' ;
5
5
6
- p5 . Metro = function ( ) {
7
- this . clock = new Clock ( {
8
- callback : this . ontick . bind ( this ) ,
9
- } ) ;
10
- this . syncedParts = [ ] ;
11
- this . bpm = 120 ; // gets overridden by p5.Part
12
- this . _init ( ) ;
6
+ class Metro {
7
+ constructor ( ) {
8
+ this . clock = new Clock ( {
9
+ callback : this . ontick . bind ( this ) ,
10
+ } ) ;
11
+ this . syncedParts = [ ] ;
12
+ this . bpm = 120 ; // gets overridden by p5.Part
13
+ this . _init ( ) ;
13
14
14
- this . prevTick = 0 ;
15
- this . tatumTime = 0 ;
15
+ this . prevTick = 0 ;
16
+ this . tatumTime = 0 ;
16
17
17
- this . tickCallback = function ( ) { } ;
18
- } ;
18
+ this . tickCallback = function ( ) { } ;
19
+ }
19
20
20
- p5 . Metro . prototype . ontick = function ( tickTime ) {
21
- var elapsedTime = tickTime - this . prevTick ;
22
- var secondsFromNow = tickTime - p5sound . audiocontext . currentTime ;
23
- if ( elapsedTime - this . tatumTime <= - 0.02 ) {
24
- return ;
25
- } else {
26
- // console.log('ok', this.syncedParts[0].phrases[0].name);
27
- this . prevTick = tickTime ;
21
+ ontick ( tickTime ) {
22
+ var elapsedTime = tickTime - this . prevTick ;
23
+ var secondsFromNow = tickTime - p5sound . audiocontext . currentTime ;
24
+ if ( elapsedTime - this . tatumTime <= - 0.02 ) {
25
+ return ;
26
+ } else {
27
+ // console.log('ok', this.syncedParts[0].phrases[0].name);
28
+ this . prevTick = tickTime ;
28
29
29
- // for all of the active things on the metro:
30
- var self = this ;
31
- this . syncedParts . forEach ( function ( thisPart ) {
32
- if ( ! thisPart . isPlaying ) return ;
33
- thisPart . incrementStep ( secondsFromNow ) ;
34
- // each synced source keeps track of its own beat number
35
- thisPart . phrases . forEach ( function ( thisPhrase ) {
36
- var phraseArray = thisPhrase . sequence ;
37
- var bNum = self . metroTicks % phraseArray . length ;
38
- if (
39
- phraseArray [ bNum ] !== 0 &&
40
- ( self . metroTicks < phraseArray . length || ! thisPhrase . looping )
41
- ) {
42
- thisPhrase . callback ( secondsFromNow , phraseArray [ bNum ] ) ;
43
- }
30
+ // for all of the active things on the metro:
31
+ var self = this ;
32
+ this . syncedParts . forEach ( function ( thisPart ) {
33
+ if ( ! thisPart . isPlaying ) return ;
34
+ thisPart . incrementStep ( secondsFromNow ) ;
35
+ // each synced source keeps track of its own beat number
36
+ thisPart . phrases . forEach ( function ( thisPhrase ) {
37
+ var phraseArray = thisPhrase . sequence ;
38
+ var bNum = self . metroTicks % phraseArray . length ;
39
+ if (
40
+ phraseArray [ bNum ] !== 0 &&
41
+ ( self . metroTicks < phraseArray . length || ! thisPhrase . looping )
42
+ ) {
43
+ thisPhrase . callback ( secondsFromNow , phraseArray [ bNum ] ) ;
44
+ }
45
+ } ) ;
44
46
} ) ;
45
- } ) ;
46
- this . metroTicks += 1 ;
47
- this . tickCallback ( secondsFromNow ) ;
47
+ this . metroTicks += 1 ;
48
+ this . tickCallback ( secondsFromNow ) ;
49
+ }
48
50
}
49
- } ;
50
51
51
- p5 . Metro . prototype . setBPM = function ( bpm , rampTime = 0 ) {
52
- var beatTime = 60 / ( bpm * this . tatums ) ;
53
- var now = p5sound . audiocontext . currentTime ;
54
- this . tatumTime = beatTime ;
52
+ setBPM ( bpm , rampTime = 0 ) {
53
+ var beatTime = 60 / ( bpm * this . tatums ) ;
54
+ var now = p5sound . audiocontext . currentTime ;
55
+ this . tatumTime = beatTime ;
55
56
56
- this . clock . frequency . setValueAtTime ( this . clock . frequency . value , now ) ;
57
- this . clock . frequency . linearRampToValueAtTime ( bpm , now + rampTime ) ;
58
- this . bpm = bpm ;
59
- } ;
57
+ this . clock . frequency . setValueAtTime ( this . clock . frequency . value , now ) ;
58
+ this . clock . frequency . linearRampToValueAtTime ( bpm , now + rampTime ) ;
59
+ this . bpm = bpm ;
60
+ }
60
61
61
- p5 . Metro . prototype . getBPM = function ( ) {
62
- return ( this . clock . getRate ( ) / this . tatums ) * 60 ;
63
- } ;
62
+ getBPM ( ) {
63
+ return ( this . clock . getRate ( ) / this . tatums ) * 60 ;
64
+ }
64
65
65
- p5 . Metro . prototype . _init = function ( ) {
66
- this . metroTicks = 0 ;
67
- // this.setBPM(120);
68
- } ;
66
+ _init ( ) {
67
+ this . metroTicks = 0 ;
68
+ // this.setBPM(120);
69
+ }
69
70
70
- // clear existing synced parts, add only this one
71
- p5 . Metro . prototype . resetSync = function ( part ) {
72
- this . syncedParts = [ part ] ;
73
- } ;
71
+ // clear existing synced parts, add only this one
72
+ resetSync ( part ) {
73
+ this . syncedParts = [ part ] ;
74
+ }
74
75
75
- // push a new synced part to the array
76
- p5 . Metro . prototype . pushSync = function ( part ) {
77
- this . syncedParts . push ( part ) ;
78
- } ;
76
+ // push a new synced part to the array
77
+ pushSync ( part ) {
78
+ this . syncedParts . push ( part ) ;
79
+ }
79
80
80
- p5 . Metro . prototype . start = function ( timeFromNow ) {
81
- var t = timeFromNow || 0 ;
82
- var now = p5sound . audiocontext . currentTime ;
83
- this . clock . start ( now + t ) ;
84
- this . setBPM ( this . bpm ) ;
85
- } ;
81
+ start ( timeFromNow ) {
82
+ var t = timeFromNow || 0 ;
83
+ var now = p5sound . audiocontext . currentTime ;
84
+ this . clock . start ( now + t ) ;
85
+ this . setBPM ( this . bpm ) ;
86
+ }
86
87
87
- p5 . Metro . prototype . stop = function ( timeFromNow ) {
88
- var t = timeFromNow || 0 ;
89
- var now = p5sound . audiocontext . currentTime ;
90
- this . clock . stop ( now + t ) ;
91
- } ;
88
+ stop ( timeFromNow ) {
89
+ var t = timeFromNow || 0 ;
90
+ var now = p5sound . audiocontext . currentTime ;
91
+ this . clock . stop ( now + t ) ;
92
+ }
92
93
93
- p5 . Metro . prototype . beatLength = function ( tatums ) {
94
- this . tatums = 1 / tatums / 4 ; // lowest possible division of a beat
95
- } ;
94
+ beatLength ( tatums ) {
95
+ this . tatums = 1 / tatums / 4 ; // lowest possible division of a beat
96
+ }
97
+ }
98
+ export default Metro ;
0 commit comments