1- import * as flo from "@ flo- audio/libflo-audio" ;
2- import initFlo from "@flo-audio/libflo-audio" ;
1+ // floProcessor.ts - Fixed flo audio processing
2+ import initFlo , * as flo from "@flo-audio/libflo-audio" ;
33
44let floInitialized = false ;
5+
56async function ensureFloInitialized ( ) {
67 if ( ! floInitialized ) {
78 await initFlo ( ) ;
89 floInitialized = true ;
910 }
1011}
1112
12- // Decode a .flo file buffer to AudioBuffer using @flo -audio/libflo
13+ // Decode a .flo file buffer to AudioBuffer using @flo -audio/libflo-audio
1314export async function decodeFloToAudioBuffer (
1415 floData : ArrayBuffer ,
1516 audioContext : AudioContext ,
1617) : Promise < AudioBuffer > {
1718 await ensureFloInitialized ( ) ;
19+
1820 const uint8Flo = new Uint8Array ( floData ) ;
21+
22+ // Decode flo to interleaved Float32Array samples
1923 const samples = flo . decode ( uint8Flo ) ;
24+
25+ // Get file info for audio properties
2026 const fileInfo = flo . info ( uint8Flo ) ;
2127 const { channels, sample_rate } = fileInfo ;
22- const length = samples . length / channels ;
23- const audioBuffer = audioContext . createBuffer ( channels , length , sample_rate ) ;
24- // Deinterleave
28+
29+ // Calculate number of frames
30+ const frameCount = samples . length / channels ;
31+
32+ // Create AudioBuffer
33+ const audioBuffer = audioContext . createBuffer (
34+ channels ,
35+ frameCount ,
36+ sample_rate
37+ ) ;
38+
39+ // Deinterleave samples into separate channels
2540 for ( let ch = 0 ; ch < channels ; ch ++ ) {
2641 const channelData = audioBuffer . getChannelData ( ch ) ;
27- for ( let i = 0 ; i < length ; i ++ ) {
42+ for ( let i = 0 ; i < frameCount ; i ++ ) {
2843 channelData [ i ] = samples [ i * channels + ch ] ;
2944 }
3045 }
46+
3147 return audioBuffer ;
3248}
3349
34- // Get FLO file info (sample rate, channels, bit depth, duration, etc)
35- export async function getFloInfo ( floData : ArrayBuffer ) : Promise < any > {
50+ // Get flo file info (returns object with sample_rate, channels, bit_depth, etc.)
51+ export async function getFloInfo ( floData : ArrayBuffer ) : Promise < {
52+ sample_rate : number ;
53+ channels : number ;
54+ bit_depth : number ;
55+ duration_secs : number ;
56+ is_lossy : boolean ;
57+ compression_ratio ?: number ;
58+ } > {
3659 await ensureFloInitialized ( ) ;
3760 const uint8Flo = new Uint8Array ( floData ) ;
3861 return flo . info ( uint8Flo ) ;
3962}
4063
41- // Validate FLO file integrity (CRC32)
64+ // Validate flo file integrity (CRC32)
4265export async function validateFlo ( floData : ArrayBuffer ) : Promise < boolean > {
4366 await ensureFloInitialized ( ) ;
4467 const uint8Flo = new Uint8Array ( floData ) ;
4568 return flo . validate ( uint8Flo ) ;
4669}
4770
48- // Extract metadata as JS object (title, artist, album, etc )
71+ // Extract metadata as JS object (returns null if no metadata )
4972export async function getFloMetadata (
5073 floData : ArrayBuffer ,
51- ) : Promise < any | null > {
74+ ) : Promise < {
75+ title ?: string ;
76+ artist ?: string ;
77+ album ?: string ;
78+ [ key : string ] : any ;
79+ } | null > {
5280 await ensureFloInitialized ( ) ;
5381 const uint8Flo = new Uint8Array ( floData ) ;
5482 return flo . get_metadata ( uint8Flo ) ;
5583}
5684
57- // Extract cover art (returns { mime_type, data })
85+ // Extract cover art (returns { mime_type, data } or null )
5886export async function getFloCoverArt (
5987 floData : ArrayBuffer ,
6088) : Promise < { mime_type : string ; data : Uint8Array } | null > {
@@ -63,7 +91,7 @@ export async function getFloCoverArt(
6391 return flo . get_cover_art ( uint8Flo ) ;
6492}
6593
66- // Extract synchronized lyrics (returns array or null)
94+ // Extract synchronized lyrics (returns array of { timestamp_ms, text } or null)
6795export async function getFloSyncedLyrics (
6896 floData : ArrayBuffer ,
6997) : Promise < Array < { timestamp_ms : number ; text : string } > | null > {
@@ -72,10 +100,13 @@ export async function getFloSyncedLyrics(
72100 return flo . get_synced_lyrics ( uint8Flo ) ;
73101}
74102
75- // Create FLO metadata from JS object (returns Uint8Array)
103+ // Create flo metadata from JS object (returns Uint8Array)
76104export async function createFloMetadataFromObject (
77105 obj : any ,
78106) : Promise < Uint8Array > {
79107 await ensureFloInitialized ( ) ;
80108 return flo . create_metadata_from_object ( obj ) ;
81109}
110+
111+ // Export initialization function
112+ export { initFlo , ensureFloInitialized } ;
0 commit comments