@@ -7,19 +7,28 @@ define(function (require) {
7
7
p5 . SoundLoop = function ( callback , interval , BPM ) {
8
8
9
9
this . callback = callback ;
10
+
11
+ //set private variables
10
12
this . _interval = interval ;
11
- this . _timeSignature = 4 ;
12
13
13
- this . bpm = BPM || 60 ;
14
+ //musicalTimeMode is true if time is specified as "4n", "8n" ...etc
15
+ this . musicalTimeMode = typeof this . _interval === 'number' ? false : true ;
14
16
17
+ //These variables should only be modified if using musicalTimeMode
18
+ //If these variables are modified when interval is measured in seconds,
19
+ //the interval will become inaccurate.
20
+ //ex. 8 second interval at 60BPM in 4/4 time will be 8 seconds long
21
+ //8 second interval at 120BPM in 5/4 time will be 3.2 seconds long
22
+ this . _timeSignature = 4 ;
23
+ this . _bpm = BPM || 60 ;
15
24
16
25
var self = this ;
17
26
this . clock = new Clock ( {
18
27
'callback' : function ( time ) {
19
28
var timeFromNow = time - p5sound . audiocontext . currentTime ;
20
29
self . callback ( timeFromNow ) ;
21
30
} ,
22
- 'frequency' : this . calcFreq ( this . _interval )
31
+ 'frequency' : this . calcFreq ( )
23
32
} ) ;
24
33
} ;
25
34
@@ -40,29 +49,20 @@ define(function (require) {
40
49
this . clock . pause ( time ) ;
41
50
} ;
42
51
43
- p5 . SoundLoop . prototype . setBPM = function ( bpm ) {
44
- this . bpm = bpm ;
45
- this . changeInterval ( this . _interval ) ;
46
- } ;
47
-
48
- p5 . SoundLoop . prototype . setTimeSignature = function ( timeSig ) {
49
- this . _timeSignature = timeSig ;
50
- this . changeInterval ( this . _interval ) ;
51
- } ;
52
-
53
- p5 . SoundLoop . prototype . changeInterval = function ( newInterval ) {
54
- this . _interval = newInterval ;
55
- this . clock . frequency . value = this . calcFreq ( this . _interval ) ;
52
+ p5 . SoundLoop . prototype . _update = function ( ) {
53
+ this . clock . frequency . value = this . calcFreq ( ) ;
56
54
} ;
57
55
58
- p5 . SoundLoop . prototype . calcFreq = function ( interval ) {
59
- if ( typeof interval === 'number' ) {
60
- return this . bpm / 60 / interval * ( this . _timeSignature / 4 ) ;
61
- } else if ( typeof interval === 'string' ) {
62
- return this . bpm / 60 / this . _convertNotation ( interval ) * this . _timeSignature / 4 ;
56
+ p5 . SoundLoop . prototype . calcFreq = function ( ) {
57
+ if ( typeof this . _interval === 'number' ) {
58
+ console . log ( this . _interval ) ;
59
+ return this . _bpm / 60 / this . _interval * ( this . _timeSignature / 4 ) ;
60
+ } else if ( typeof this . _interval === 'string' ) {
61
+ return this . _bpm / 60 / this . _convertNotation ( this . _interval ) * ( this . _timeSignature / 4 ) ;
63
62
}
64
63
} ;
65
64
65
+ //TIME NOTATION FUNCS
66
66
p5 . SoundLoop . prototype . _convertNotation = function ( value ) {
67
67
var type = value . slice ( - 1 ) ;
68
68
value = Number ( value . slice ( 0 , - 1 ) ) ;
@@ -77,14 +77,60 @@ define(function (require) {
77
77
78
78
} ;
79
79
80
+
80
81
p5 . SoundLoop . prototype . _measure = function ( value ) {
81
- return value ;
82
+ return value * this . _timeSignature ;
82
83
} ;
83
84
84
85
p5 . SoundLoop . prototype . _note = function ( value ) {
85
- return 1 / value ;
86
+ return this . _timeSignature / value ;
86
87
} ;
87
88
89
+ // PUBLIC VARIABLES
90
+ Object . defineProperty ( p5 . SoundLoop . prototype , 'bpm' , {
91
+ get : function ( ) {
92
+ return this . _bpm ;
93
+ } ,
94
+ set : function ( bpm ) {
95
+ if ( ! this . musicalTimeMode ) {
96
+ console . warn ( 'Changing the BPM in "seconds" mode is not advised. ' +
97
+ 'This will make the specified time interval inaccurate. ' +
98
+ '8 second interval at 60BPM in 4/4 time will be 8 seconds long ' +
99
+ '8 second interval at 120BPM in 5/4 time will be 3.2 seconds long. ' +
100
+ 'Use musical timing notation ("2n", "4n", "1m"...etc' ) ;
101
+ }
102
+ this . _bpm = bpm ;
103
+ this . _update ( ) ;
104
+ }
105
+ } ) ;
106
+
107
+ Object . defineProperty ( p5 . SoundLoop . prototype , 'timeSignature' , {
108
+ get : function ( ) {
109
+ return this . _timeSignature ;
110
+ } ,
111
+ set : function ( timeSig ) {
112
+ if ( ! this . musicalTimeMode ) {
113
+ console . warn ( 'Changing the time signature in "seconds" mode is not advised. ' +
114
+ 'This will make the specified time interval inaccurate. ' +
115
+ '8 second interval at 60BPM in 4/4 time will be 8 seconds long ' +
116
+ '8 second interval at 120BPM in 5/4 time will be 3.2 seconds long. ' +
117
+ 'Use musical timing notation ("2n", "4n", "1m"...etc' ) ;
118
+ }
119
+ this . _timeSignature = timeSig ;
120
+ this . _update ( ) ;
121
+ }
122
+ } ) ;
123
+
124
+ Object . defineProperty ( p5 . SoundLoop . prototype , 'interval' , {
125
+ get : function ( ) {
126
+ return this . _interval ;
127
+ } ,
128
+ set : function ( interval ) {
129
+ this . _interval = interval ;
130
+ this . _update ( ) ;
131
+ }
132
+ } ) ;
133
+
88
134
89
135
return p5 . SoundLoop ;
90
136
0 commit comments