32
32
import AudioMotionAnalyzer from 'audiomotion-analyzer' ;
33
33
import packageJson from '../package.json' ;
34
34
import * as fileExplorer from './file-explorer.js' ;
35
- import * as mm from 'music-metadata-browser ' ;
35
+ import { parseBlob , parseBuffer , parseWebStream } from 'music-metadata' ;
36
36
import './scrollIntoViewIfNeeded-polyfill.js' ;
37
37
import { get , set , del } from 'idb-keyval' ;
38
38
@@ -2120,7 +2120,8 @@ function loadGradientIntoCurrentGradient(gradientKey) {
2120
2120
/**
2121
2121
* Load a music file from the user's computer
2122
2122
*/
2123
- function loadLocalFile ( obj ) {
2123
+ async function loadLocalFile ( obj ) {
2124
+
2124
2125
const fileBlob = obj . files [ 0 ] ;
2125
2126
2126
2127
if ( fileBlob ) {
@@ -2129,11 +2130,14 @@ function loadLocalFile( obj ) {
2129
2130
audioEl . dataset . file = fileBlob . name ;
2130
2131
audioEl . dataset . title = parsePath ( fileBlob . name ) . baseName ;
2131
2132
2132
- // load and play
2133
- loadFileBlob ( fileBlob , audioEl , true )
2134
- . then ( url => mm . fetchFromUrl ( url ) )
2135
- . then ( metadata => addMetadata ( metadata , audioEl ) )
2136
- . catch ( e => { } ) ;
2133
+ try {
2134
+ await loadFileBlob ( fileBlob , audioEl , true ) ;
2135
+ // Maybe do this parallel?
2136
+ const metadata = await parseBlob ( fileBlob ) ;
2137
+ await addMetadata ( metadata , audioEl ) ;
2138
+ } catch ( error ) {
2139
+ consoleLog ( "Failed to load local file" , error ) ;
2140
+ }
2137
2141
}
2138
2142
}
2139
2143
@@ -3306,47 +3310,45 @@ async function retrieveMetadata() {
3306
3310
3307
3311
if ( queueItem ) {
3308
3312
3309
- let uri = queueItem . dataset . file ,
3310
- revoke = false ;
3313
+ let uri = queueItem . dataset . file ;
3314
+ let file ;
3311
3315
3312
3316
waitingMetadata ++ ;
3313
3317
delete queueItem . dataset . retrieve ;
3318
+ let metadata ;
3314
3319
3315
- queryMetadata: {
3316
- if ( queueItem . handle ) {
3317
- try {
3318
- if ( await queueItem . handle . requestPermission ( ) != 'granted' )
3319
- break queryMetadata;
3320
+ if ( queueItem . handle ) {
3321
+ // Fetch metadata from File object
3322
+ if ( await queueItem . handle . requestPermission ( ) !== 'granted' )
3323
+ return ;
3320
3324
3321
- uri = URL . createObjectURL ( await queueItem . handle . getFile ( ) ) ;
3322
- revoke = true ;
3323
- }
3324
- catch ( e ) {
3325
- break queryMetadata;
3326
- }
3325
+ file = await queueItem . handle . getFile ( ) ;
3326
+ uri = URL . createObjectURL ( file ) ;
3327
+ metadata = await parseBlob ( file ) ;
3328
+ } else {
3329
+ // Fetch metadata from URI
3330
+ const response = await fetch ( uri ) ;
3331
+ if ( response . body && typeof response . body . getReader === 'function' ) {
3332
+ metadata = await parseWebStream ( response . body , { skipPostHeaders : true } ) ;
3333
+ } else {
3334
+ // Fallback for Safari, as readable byte streams are not supported by Safari
3335
+ metadata = await parseBlob ( await response . blob ( ) ) ;
3327
3336
}
3337
+ }
3328
3338
3329
- try {
3330
- const metadata = await mm . fetchFromUrl ( uri , { skipPostHeaders : true } ) ;
3331
- if ( metadata ) {
3332
- addMetadata ( metadata , queueItem ) ; // add metadata to play queue item
3333
- syncMetadataToAudioElements ( queueItem ) ;
3334
- if ( ! ( metadata . common . picture && metadata . common . picture . length ) ) {
3335
- getFolderCover ( queueItem ) . then ( cover => {
3336
- queueItem . dataset . cover = cover ;
3337
- syncMetadataToAudioElements ( queueItem ) ;
3338
- } ) ;
3339
- }
3340
- }
3341
- }
3342
- catch ( e ) { }
3339
+ addMetadata ( metadata , queueItem ) ; // add metadata to play queue item
3340
+ syncMetadataToAudioElements ( queueItem ) ;
3341
+ if ( ! queueItem . handle && ! ( metadata . common . picture && metadata . common . picture . length ) ) {
3342
+ queueItem . dataset . cover = await getFolderCover ( uri ) ;
3343
+ syncMetadataToAudioElements ( queueItem ) ;
3344
+ }
3343
3345
3344
- if ( revoke )
3345
- URL . revokeObjectURL ( uri ) ;
3346
+ if ( file ) {
3347
+ URL . revokeObjectURL ( uri ) ;
3346
3348
}
3347
3349
3348
3350
waitingMetadata -- ;
3349
- retrieveMetadata ( ) ; // call again to continue processing the queue
3351
+ await retrieveMetadata ( ) ; // call again to continue processing the queue
3350
3352
}
3351
3353
}
3352
3354
0 commit comments