@@ -36,12 +36,22 @@ public sealed class WaveFileStreamReader : IStreamReader
36
36
private readonly long dataStart ;
37
37
private readonly long dataLength ;
38
38
39
+ private readonly List < Delegate > audioTargets = new List < Delegate > ( ) ;
40
+ private readonly List < Delegate > audioIndexTargets = new List < Delegate > ( ) ;
41
+
39
42
private int sequenceId = 0 ;
40
43
private byte [ ] buffer ;
41
44
private TimeInterval seekInterval = TimeInterval . Infinite ;
42
45
43
- private List < Delegate > audioTargets = new List < Delegate > ( ) ;
44
- private List < Delegate > audioIndexTargets = new List < Delegate > ( ) ;
46
+ /// <summary>
47
+ /// Initializes a new instance of the <see cref="WaveFileStreamReader"/> class.
48
+ /// </summary>
49
+ /// <param name="name">Name of the WAVE file.</param>
50
+ /// <param name="path">Path of the WAVE file.</param>
51
+ public WaveFileStreamReader ( string name , string path )
52
+ : this ( name , path , DateTime . UtcNow , DefaultAudioBufferSizeMs )
53
+ {
54
+ }
45
55
46
56
/// <summary>
47
57
/// Initializes a new instance of the <see cref="WaveFileStreamReader"/> class.
@@ -50,35 +60,26 @@ public sealed class WaveFileStreamReader : IStreamReader
50
60
/// <param name="path">Path of the WAVE file.</param>
51
61
/// <param name="startTime">Starting time for streams of data..</param>
52
62
/// <param name="audioBufferSizeMs">The size of each data buffer in milliseconds.</param>
53
- public WaveFileStreamReader ( string name , string path , DateTime startTime , int audioBufferSizeMs = DefaultAudioBufferSizeMs )
63
+ internal WaveFileStreamReader ( string name , string path , DateTime startTime , int audioBufferSizeMs = DefaultAudioBufferSizeMs )
54
64
{
55
65
this . Name = name ;
56
66
this . Path = path ;
57
67
this . startTime = startTime ;
58
68
var file = System . IO . Path . Combine ( path , name ) ;
69
+ this . Size = file . Length ;
59
70
this . waveFileReader = new BinaryReader ( new FileStream ( file , FileMode . Open , FileAccess . Read , FileShare . Read ) ) ;
60
71
this . waveFormat = WaveFileHelper . ReadWaveFileHeader ( this . waveFileReader ) ;
61
- this . dataLength = ( int ) WaveFileHelper . ReadWaveDataLength ( this . waveFileReader ) ;
72
+ this . dataLength = WaveFileHelper . ReadWaveDataLength ( this . waveFileReader ) ;
62
73
this . dataStart = this . waveFileReader . BaseStream . Position ;
63
74
var bufferSize = ( int ) ( this . waveFormat . AvgBytesPerSec * audioBufferSizeMs / 1000 ) ;
64
75
this . buffer = new byte [ bufferSize ] ;
65
76
66
77
// Compute originating times based on audio chunk start time + duration
67
78
var endTime = this . startTime . AddSeconds ( ( double ) this . dataLength / ( double ) this . waveFormat . AvgBytesPerSec ) ;
68
- this . MessageOriginatingTimeInterval = this . MessageCreationTimeInterval = new TimeInterval ( this . startTime , endTime ) ;
79
+ this . MessageOriginatingTimeInterval = this . MessageCreationTimeInterval = this . StreamTimeInterval = new TimeInterval ( this . startTime , endTime ) ;
69
80
70
- var messageCount = ( int ) Math . Ceiling ( ( double ) this . dataLength / bufferSize ) ;
71
- this . audioStreamMetadata = new WaveAudioStreamMetadata ( AudioStreamName , typeof ( AudioBuffer ) . AssemblyQualifiedName , name , path , this . startTime , endTime , ( int ) this . dataLength / messageCount , messageCount , audioBufferSizeMs ) ;
72
- }
73
-
74
- /// <summary>
75
- /// Initializes a new instance of the <see cref="WaveFileStreamReader"/> class.
76
- /// </summary>
77
- /// <param name="name">Name of the WAVE file.</param>
78
- /// <param name="path">Path of the WAVE file.</param>
79
- public WaveFileStreamReader ( string name , string path )
80
- : this ( name , path , DateTime . UtcNow , DefaultAudioBufferSizeMs )
81
- {
81
+ var messageCount = ( long ) Math . Ceiling ( ( double ) this . dataLength / bufferSize ) ;
82
+ this . audioStreamMetadata = new WaveAudioStreamMetadata ( AudioStreamName , typeof ( AudioBuffer ) . AssemblyQualifiedName , name , path , this . startTime , endTime , messageCount , ( double ) this . dataLength / messageCount , audioBufferSizeMs ) ;
82
83
}
83
84
84
85
/// <inheritdoc />
@@ -102,6 +103,15 @@ public IEnumerable<IStreamMetadata> AvailableStreams
102
103
/// <inheritdoc />
103
104
public TimeInterval MessageOriginatingTimeInterval { get ; private set ; }
104
105
106
+ /// <inheritdoc />
107
+ public TimeInterval StreamTimeInterval { get ; private set ; }
108
+
109
+ /// <inheritdoc/>
110
+ public long ? Size { get ; }
111
+
112
+ /// <inheritdoc/>
113
+ public int ? StreamCount => 1 ;
114
+
105
115
/// <inheritdoc />
106
116
public bool ContainsStream ( string name )
107
117
{
@@ -128,7 +138,7 @@ public T GetSupplementalMetadata<T>(string streamName)
128
138
129
139
if ( typeof ( T ) != typeof ( WaveFormat ) )
130
140
{
131
- throw new ArgumentException ( "The Audio stream supports only supplemental metadata of type WaveFormat." ) ;
141
+ throw new NotSupportedException ( "The Audio stream supports only supplemental metadata of type WaveFormat." ) ;
132
142
}
133
143
134
144
return ( T ) ( object ) this . waveFormat ;
@@ -161,20 +171,40 @@ public IStreamReader OpenNew()
161
171
}
162
172
163
173
/// <inheritdoc />
164
- public IStreamMetadata OpenStream < T > ( string name , Action < T , Envelope > target , Func < T > allocator = null , Action < SerializationException > errorHandler = null )
174
+ public IStreamMetadata OpenStream < T > ( string name , Action < T , Envelope > target , Func < T > allocator = null , Action < T > deallocator = null , Action < SerializationException > errorHandler = null )
165
175
{
166
176
ValidateStreamName ( name ) ;
167
177
178
+ if ( target == null )
179
+ {
180
+ throw new ArgumentNullException ( nameof ( target ) ) ;
181
+ }
182
+
183
+ if ( allocator != null )
184
+ {
185
+ throw new NotSupportedException ( $ "Allocators are not supported by { nameof ( WaveFileStreamReader ) } and must be null.") ;
186
+ }
187
+
168
188
// targets are later called when data is read by MoveNext or ReadAll (see InvokeTargets).
169
189
this . audioTargets . Add ( target ) ;
170
190
return this . audioStreamMetadata ;
171
191
}
172
192
173
193
/// <inheritdoc />
174
- public IStreamMetadata OpenStreamIndex < T > ( string name , Action < Func < IStreamReader , T > , Envelope > target )
194
+ public IStreamMetadata OpenStreamIndex < T > ( string name , Action < Func < IStreamReader , T > , Envelope > target , Func < T > allocator = null )
175
195
{
176
196
ValidateStreamName ( name ) ;
177
197
198
+ if ( target == null )
199
+ {
200
+ throw new ArgumentNullException ( nameof ( target ) ) ;
201
+ }
202
+
203
+ if ( allocator != null )
204
+ {
205
+ throw new NotSupportedException ( $ "Allocators are not supported by { nameof ( WaveFileStreamReader ) } and must be null.") ;
206
+ }
207
+
178
208
// targets are later called when data is read by MoveNext or ReadAll (see InvokeTargets).
179
209
this . audioIndexTargets . Add ( target ) ;
180
210
return this . audioStreamMetadata ;
@@ -222,7 +252,7 @@ private static void ValidateStreamName(string name)
222
252
if ( name != AudioStreamName )
223
253
{
224
254
// the only supported stream is the single audio stream.
225
- throw new ArgumentException ( $ "Only '{ AudioStreamName } ' stream is supported.") ;
255
+ throw new NotSupportedException ( $ "Only '{ AudioStreamName } ' stream is supported.") ;
226
256
}
227
257
}
228
258
@@ -327,11 +357,11 @@ public class WaveAudioStreamMetadata : StreamMetadataBase
327
357
/// <param name="partitionPath">Partition/file path.</param>
328
358
/// <param name="first">First message time.</param>
329
359
/// <param name="last">Last message time.</param>
330
- /// <param name="averageMessageSize">Average message size (bytes).</param>
331
360
/// <param name="messageCount">Total message count.</param>
332
- /// <param name="averageLatency">Average message latency.</param>
333
- public WaveAudioStreamMetadata ( string name , string typeName , string partitionName , string partitionPath , DateTime first , DateTime last , int averageMessageSize , int messageCount , int averageLatency )
334
- : base ( name , AudioSourceId , typeName , partitionName , partitionPath , first , last , averageMessageSize , messageCount , averageLatency )
361
+ /// <param name="averageMessageSize">Average message size (bytes).</param>
362
+ /// <param name="averageLatencyMs">Average message latency (milliseconds).</param>
363
+ internal WaveAudioStreamMetadata ( string name , string typeName , string partitionName , string partitionPath , DateTime first , DateTime last , long messageCount , double averageMessageSize , double averageLatencyMs )
364
+ : base ( name , AudioSourceId , typeName , partitionName , partitionPath , first , last , messageCount , averageMessageSize , averageLatencyMs )
335
365
{
336
366
}
337
367
}
0 commit comments