@@ -14,6 +14,7 @@ namespace TuneLab.Data;
1414internal class AudioPart : Part , IAudioPart
1515{
1616 public IActionEvent AudioChanged => mAudioChanged ;
17+ public INotifiableProperty < string > BaseDirectory { get ; } = new NotifiableProperty < string > ( string . Empty ) ;
1718 public override DataString Name { get ; }
1819 public override DataStruct < double > Pos { get ; }
1920 public override DataStruct < double > Dur { get ; }
@@ -27,56 +28,32 @@ public AudioPart(ITrack track, AudioPartInfo info) : base(track)
2728 Dur = new ( this ) ;
2829 Path = new ( this , string . Empty ) ;
2930 Dur . Modified . Subscribe ( mDurationChanged ) ;
30- Path . Modified . Subscribe ( async ( ) =>
31- {
32- mAudioData = null ;
33- mWaveforms = [ ] ;
34- mAudioChanged . Invoke ( ) ;
35- IAudioData ? audioData = null ;
36- Waveform [ ] ? waveforms = null ;
37- await Task . Run ( ( ) =>
38- {
39- try
40- {
41- int samplingRate = AudioEngine . SamplingRate ;
42- var data = AudioUtils . Decode ( Path , ref samplingRate ) ;
43- switch ( data . Length )
44- {
45- case 1 :
46- audioData = new MonoAudioData ( data [ 0 ] ) ;
47- waveforms = [ new ( data [ 0 ] ) ] ;
48- break ;
49- case 2 :
50- audioData = new StereoAudioData ( data [ 0 ] , data [ 1 ] ) ;
51- waveforms = [ new ( data [ 0 ] ) , new ( data [ 1 ] ) ] ;
52- break ;
53- }
54- }
55- catch ( Exception ex )
56- {
57- audioData = null ;
58- waveforms = null ;
59- }
60- } ) ;
61-
62- if ( audioData == null || waveforms == null )
63- return ;
64-
65- mAudioData = audioData ;
66- mWaveforms = waveforms ;
67- mAudioChanged . Invoke ( ) ;
31+ Path . Modified . Subscribe ( Reload ) ;
32+ BaseDirectory . Modified . Subscribe ( ( ) =>
33+ {
34+ if ( Path . Value . StartsWith ( ".." ) )
35+ Reload ( ) ;
6836 } ) ;
6937 IDataObject < AudioPartInfo > . SetInfo ( this , info ) ;
7038 }
7139
7240 public override AudioPartInfo GetInfo ( )
7341 {
42+ var path = Path . Value ;
43+ if ( ! string . IsNullOrEmpty ( BaseDirectory . Value ) )
44+ {
45+ if ( path . StartsWith ( BaseDirectory . Value ) )
46+ {
47+ path = ".." + path [ BaseDirectory . Value . Length ..] ;
48+ }
49+ }
50+
7451 return new ( )
7552 {
7653 Name = Name ,
7754 Pos = Pos ,
7855 Dur = Dur ,
79- Path = Path
56+ Path = path
8057 } ;
8158 }
8259
@@ -106,6 +83,51 @@ protected override int SampleCount()
10683 return mAudioData == null ? 0 : Math . Min ( base . SampleCount ( ) , mAudioData . Count ) ;
10784 }
10885
86+ async void Reload ( )
87+ {
88+ mAudioData = null ;
89+ mWaveforms = [ ] ;
90+ mAudioChanged . Invoke ( ) ;
91+ IAudioData ? audioData = null ;
92+ Waveform [ ] ? waveforms = null ;
93+ await Task . Run ( ( ) =>
94+ {
95+ try
96+ {
97+ string path = Path ;
98+ if ( path . StartsWith ( ".." ) )
99+ {
100+ path = System . IO . Path . Combine ( BaseDirectory . Value , path [ 3 ..] ) ;
101+ }
102+ int samplingRate = AudioEngine . SamplingRate ;
103+ var data = AudioUtils . Decode ( path , ref samplingRate ) ;
104+ switch ( data . Length )
105+ {
106+ case 1 :
107+ audioData = new MonoAudioData ( data [ 0 ] ) ;
108+ waveforms = [ new ( data [ 0 ] ) ] ;
109+ break ;
110+ case 2 :
111+ audioData = new StereoAudioData ( data [ 0 ] , data [ 1 ] ) ;
112+ waveforms = [ new ( data [ 0 ] ) , new ( data [ 1 ] ) ] ;
113+ break ;
114+ }
115+ }
116+ catch ( Exception ex )
117+ {
118+ audioData = null ;
119+ waveforms = null ;
120+ }
121+ } ) ;
122+
123+ if ( audioData == null || waveforms == null )
124+ return ;
125+
126+ mAudioData = audioData ;
127+ mWaveforms = waveforms ;
128+ mAudioChanged . Invoke ( ) ;
129+ }
130+
109131 protected override int SamplingRate => AudioEngine . SamplingRate ;
110132 public int ChannelCount => mWaveforms . Length ;
111133
0 commit comments