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 , parseWebStream } from 'music-metadata' ;
36
36
import './scrollIntoViewIfNeeded-polyfill.js' ;
37
37
import { get , set , del } from 'idb-keyval' ;
38
38
@@ -2072,7 +2072,8 @@ function loadGradientIntoCurrentGradient(gradientKey) {
2072
2072
/**
2073
2073
* Load a music file from the user's computer
2074
2074
*/
2075
- function loadLocalFile ( obj ) {
2075
+ async function loadLocalFile ( obj ) {
2076
+
2076
2077
const fileBlob = obj . files [ 0 ] ;
2077
2078
2078
2079
if ( fileBlob ) {
@@ -2081,11 +2082,14 @@ function loadLocalFile( obj ) {
2081
2082
audioEl . dataset . file = fileBlob . name ;
2082
2083
audioEl . dataset . title = parsePath ( fileBlob . name ) . baseName ;
2083
2084
2084
- // load and play
2085
- loadFileBlob ( fileBlob , audioEl , true )
2086
- . then ( url => mm . fetchFromUrl ( url ) )
2087
- . then ( metadata => addMetadata ( metadata , audioEl ) )
2088
- . catch ( e => { } ) ;
2085
+ try {
2086
+ await loadFileBlob ( fileBlob , audioEl , true ) ;
2087
+ // Maybe do this parallel?
2088
+ const metadata = await parseBlob ( fileBlob ) ;
2089
+ await addMetadata ( metadata , audioEl ) ;
2090
+ } catch ( error ) {
2091
+ consoleLog ( "Failed to load local file" , error ) ;
2092
+ }
2089
2093
}
2090
2094
}
2091
2095
@@ -3247,47 +3251,44 @@ async function retrieveMetadata() {
3247
3251
3248
3252
if ( queueItem ) {
3249
3253
3250
- let uri = queueItem . dataset . file ,
3251
- revoke = false ;
3254
+ let uri = queueItem . dataset . file ;
3255
+ let file ;
3252
3256
3253
3257
waitingMetadata ++ ;
3254
3258
delete queueItem . dataset . retrieve ;
3259
+ let metadata ;
3255
3260
3256
- queryMetadata: {
3257
- if ( queueItem . handle ) {
3258
- try {
3259
- if ( await queueItem . handle . requestPermission ( ) != 'granted' )
3260
- break queryMetadata;
3261
+ if ( queueItem . handle ) {
3262
+ // Fetch metadata from File object
3263
+ if ( await queueItem . handle . requestPermission ( ) !== 'granted' )
3264
+ return ;
3261
3265
3262
- uri = URL . createObjectURL ( await queueItem . handle . getFile ( ) ) ;
3263
- revoke = true ;
3264
- }
3265
- catch ( e ) {
3266
- break queryMetadata;
3267
- }
3266
+ file = await queueItem . handle . getFile ( ) ;
3267
+ uri = URL . createObjectURL ( file ) ;
3268
+ metadata = await parseBlob ( file , { skipPostHeaders : true } ) ;
3269
+ } else {
3270
+ // Fetch metadata from URI
3271
+ const response = await fetch ( uri ) ;
3272
+ if ( response . body ) {
3273
+ metadata = await parseWebStream ( response . body , { skipPostHeaders : true } ) ;
3274
+ } else {
3275
+ throw new Error ( 'Failed to stream response.body' ) ;
3268
3276
}
3277
+ }
3269
3278
3270
- try {
3271
- const metadata = await mm . fetchFromUrl ( uri , { skipPostHeaders : true } ) ;
3272
- if ( metadata ) {
3273
- addMetadata ( metadata , queueItem ) ; // add metadata to play queue item
3274
- syncMetadataToAudioElements ( queueItem ) ;
3275
- if ( ! ( metadata . common . picture && metadata . common . picture . length ) ) {
3276
- getFolderCover ( queueItem ) . then ( cover => {
3277
- queueItem . dataset . cover = cover ;
3278
- syncMetadataToAudioElements ( queueItem ) ;
3279
- } ) ;
3280
- }
3281
- }
3282
- }
3283
- catch ( e ) { }
3279
+ addMetadata ( metadata , queueItem ) ; // add metadata to play queue item
3280
+ syncMetadataToAudioElements ( queueItem ) ;
3281
+ if ( ! queueItem . handle && ! ( metadata . common . picture && metadata . common . picture . length ) ) {
3282
+ queueItem . dataset . cover = await getFolderCover ( uri ) ;
3283
+ syncMetadataToAudioElements ( queueItem ) ;
3284
+ }
3284
3285
3285
- if ( revoke )
3286
- URL . revokeObjectURL ( uri ) ;
3286
+ if ( file ) {
3287
+ URL . revokeObjectURL ( uri ) ;
3287
3288
}
3288
3289
3289
3290
waitingMetadata -- ;
3290
- retrieveMetadata ( ) ; // call again to continue processing the queue
3291
+ await retrieveMetadata ( ) ; // call again to continue processing the queue
3291
3292
}
3292
3293
}
3293
3294
0 commit comments