File tree 4 files changed +33
-9
lines changed
4 files changed +33
-9
lines changed Original file line number Diff line number Diff line change @@ -68,16 +68,16 @@ function Audio(source, options) {
68
68
this . buffer = source
69
69
}
70
70
71
- // audiobuffer case
72
- else if ( isAudioBuffer ( source ) ) {
73
- this . buffer = new AudioBufferList ( source )
74
- }
75
-
76
71
// other Audio instance
77
72
else if ( source instanceof Audio ) {
78
73
this . buffer = source . buffer . clone ( )
79
74
}
80
75
76
+ // audiobuffer case
77
+ else if ( isAudioBuffer ( source ) ) {
78
+ this . buffer = new AudioBufferList ( source )
79
+ }
80
+
81
81
// array with malformed data
82
82
else if ( isMultisource ( source ) ) {
83
83
throw Error ( 'Bad argument. Use `Audio.from` to create joined audio.' )
Original file line number Diff line number Diff line change 53
53
"compute-qmean" : " ^1.0.0" ,
54
54
"decibels" : " ^2.0.0" ,
55
55
"fourier-transform" : " ^1.0.2" ,
56
- "is-audio-buffer" : " ^1.0.11 " ,
56
+ "is-audio-buffer" : " ^1.1.0 " ,
57
57
"is-browser" : " ^2.0.1" ,
58
58
"is-promise" : " ^2.1.0" ,
59
59
"is-relative" : " ^1.0.0" ,
Original file line number Diff line number Diff line change @@ -136,7 +136,9 @@ Audio.load = function load (source, callback) {
136
136
// if source is cached but loading - just clone when loaded
137
137
if ( isPromise ( Audio . cache [ source ] ) ) {
138
138
promise = Audio . cache [ source ] . then ( audio => {
139
- audio = Audio ( audio )
139
+ // in order to avoid fetching modified source, we clone cached source
140
+ audio = Audio ( Audio . cache [ source ] )
141
+
140
142
callback && callback ( null , audio )
141
143
return Promise . resolve ( audio )
142
144
} , error => {
@@ -154,7 +156,10 @@ Audio.load = function load (source, callback) {
154
156
else {
155
157
promise = loadAudio ( source ) . then ( audioBuffer => {
156
158
let audio = Audio ( audioBuffer )
157
- Audio . cache [ source ] = audio
159
+
160
+ // since user may modify original audio later, we have to put clone into cache
161
+ Audio . cache [ source ] = Audio ( audio )
162
+
158
163
callback && callback ( null , audio )
159
164
return Promise . resolve ( audio )
160
165
} , error => {
Original file line number Diff line number Diff line change @@ -221,11 +221,30 @@ t('load caching', t => {
221
221
222
222
//put into cache
223
223
Audio . load ( localWav ) . then ( ( audio ) => {
224
+ a = audio
225
+
226
+ a . slice ( 0 , 1 ) . save ( 'x.wav' , e => {
227
+ if ( ! isBrowser ) {
228
+ let p = __dirname + path . sep + 'x.wav'
229
+ t . ok ( fs . existsSync ( p ) )
230
+ fs . unlinkSync ( p ) ;
231
+ }
232
+ } )
233
+
234
+
224
235
t . ok ( audio )
225
236
} )
226
237
227
238
//load once first item is loaded
228
- Audio . load ( localWav ) . then ( ( audio ) => {
239
+ Audio . load ( localWav , ( err , audio ) => {
240
+ audio . slice ( 1 , 2 ) . save ( 'y.wav' , e => {
241
+ if ( ! isBrowser ) {
242
+ let p = __dirname + path . sep + 'y.wav'
243
+ t . ok ( fs . existsSync ( p ) )
244
+ fs . unlinkSync ( p ) ;
245
+ }
246
+ } )
247
+
229
248
t . ok ( Object . keys ( Audio . cache ) . length )
230
249
t . ok ( audio instanceof Audio )
231
250
t . notEqual ( audio , a )
You can’t perform that action at this time.
0 commit comments