@@ -28,7 +28,14 @@ import {
2828 spanDb ,
2929} from "./signal.js" ;
3030import { DRUM_STEM , VOICE_STEM , cachedStems , fetchStems , stemsAccess } from "./stems.js" ;
31- import { READ_VERSION , type AudibleStem , type Music , type ReadAudio , type Voice } from "./types.js" ;
31+ import {
32+ READ_VERSION ,
33+ type AudibleStem ,
34+ type Layer ,
35+ type Music ,
36+ type ReadAudio ,
37+ type Voice ,
38+ } from "./types.js" ;
3239import { voiceFromActivity , voiceFromWords , type TimedWord } from "./voice.js" ;
3340
3441export interface ReadOptions {
@@ -37,6 +44,7 @@ export interface ReadOptions {
3744 /** A transcript of the recording; `<file>.transcript.json` next to it is used when absent. */
3845 transcript ?: string ;
3946 quality ?: AnalysisQuality ;
47+ layer ?: Layer ;
4048}
4149
4250export interface ReadResult {
@@ -379,6 +387,46 @@ async function analyze(
379387 return { ...about , voice, effects, music : null , ambience : null , warnings } ;
380388}
381389
390+ async function analyzeLayer (
391+ path : string ,
392+ file : string ,
393+ sha : string ,
394+ transcript : string | null ,
395+ layer : Layer ,
396+ ) : Promise < ReadAudio > {
397+ const samples = await decodeMono ( path ) ;
398+ const about = {
399+ ...base ( file , sha , round ( samples . length / READ_RATE , 3 ) , null , transcript ) ,
400+ layer,
401+ } ;
402+ const none = { voice : null , effects : null , music : null , ambience : null , warnings : [ ] } ;
403+ if ( layer === "music" ) {
404+ const { music, ambience } = musicOf ( { music : samples } , null , samples ) ;
405+ return { ...about , ...none , music, ambience, bpm : tempoOf ( music ) } ;
406+ }
407+ if ( layer === "sfx" ) {
408+ const effects = findEffects ( {
409+ sources : [ { samples } ] ,
410+ sampleRate : READ_RATE ,
411+ mix : samples ,
412+ music : null ,
413+ beats : [ ] ,
414+ speech : [ ] ,
415+ } ) ;
416+ return { ...about , ...none , effects } ;
417+ }
418+ const words = transcript ? wordsOf ( transcript ) : [ ] ;
419+ const pitch = pitchTrack ( await decodeMono ( path , PITCH_RATE ) ) ;
420+ const levelOf = ( span : Span ) => spanDb ( samples , READ_RATE , span . start , span . end ) ;
421+ const voice = words . length
422+ ? voiceFromWords ( words , pitch , levelOf )
423+ : voiceFromActivity ( samples , READ_RATE , pitch , levelOf ) ;
424+ const warnings = words . length
425+ ? [ ]
426+ : [ `no transcript: voice lines carry no words; ${ transcribeHint ( file ) } ` ] ;
427+ return { ...about , ...none , voice, warnings } ;
428+ }
429+
382430function base (
383431 file : string ,
384432 sha : string ,
@@ -408,6 +456,7 @@ export async function readAudio(
408456 options : ReadOptions ,
409457) : Promise < ReadResult > {
410458 const quality = options . quality ?? "local" ;
459+ const { layer } = options ;
411460 const sha = await sha256 ( path ) ;
412461 const transcript = transcriptFor ( path , options . transcript ) ;
413462 const transcriptKey = fingerprint ( transcript ) ;
@@ -419,12 +468,14 @@ export async function readAudio(
419468 if ( existsSync ( jsonPath ) && existsSync ( textPath ) ) {
420469 const entry = JSON . parse ( readFileSync ( jsonPath , "utf8" ) ) as CacheEntry ;
421470 const stemsNowPossible =
471+ ! layer &&
422472 entry . analysis . separation === "mix" &&
423473 quality === "best" &&
424474 ( cachedStems ( stemDir ) !== null || typeof ( await stemsAccess ( ) ) !== "string" ) ;
425475 if (
426476 ( entry . analysis . separation !== "stems" || cachedStems ( stemDir ) !== null ) &&
427477 entry . analysis . version === READ_VERSION &&
478+ entry . analysis . layer === layer &&
428479 entry . revision === CACHE_REVISION &&
429480 entry . transcriptKey === transcriptKey &&
430481 ! stemsNowPossible &&
@@ -437,7 +488,9 @@ export async function readAudio(
437488 )
438489 return { analysis : entry . analysis , textPath, jsonPath, cached : true } ;
439490 }
440- const analysis = await analyze ( path , file , sha , transcript , stemDir , quality , options . cacheDir ) ;
491+ const analysis = layer
492+ ? await analyzeLayer ( path , file , sha , transcript , layer )
493+ : await analyze ( path , file , sha , transcript , stemDir , quality , options . cacheDir ) ;
441494 writeAtomic (
442495 jsonPath ,
443496 JSON . stringify ( { revision : CACHE_REVISION , analysis, transcriptKey } satisfies CacheEntry ) ,
0 commit comments