public static async Task <ParseResult <Mp3File> > ParseFullMetadataAsync(IBinaryStream stream) { var streamLength = await stream.GetLengthAsync().ConfigureAwait(false); stream.Position = 0; var file = new Mp3File(); var id3v2 = await Id3v2.IsPointingToHeaderAsync(stream).ConfigureAwait(false) ? await Id3v2.ParseAsync(stream).ConfigureAwait(false) : null; var collectSamplesResult = await CollectSamplesAsync(stream, streamLength, 1).ConfigureAwait(false); if (collectSamplesResult.Item1.Count == 0) { file.Metadata = new Mp3Metadata(id3v2); return(ParseResult <Mp3File> .Create(file)); } else { var firstFrame = collectSamplesResult.Item1[0]; var contentLength = streamLength - stream.Position; if (collectSamplesResult.Item2 == BitrateType.Constant) { var duration = TimeSpan.FromSeconds(Math.Ceiling((contentLength / firstFrame.Length) * firstFrame.Duration.TotalSeconds)); var bitrate = firstFrame.Bitrate; file.Metadata = new Mp3Metadata(id3v2, bitrate, duration); } else { var xingFrame = await XingFrame.CreateAsync(stream, firstFrame).ConfigureAwait(false); if (xingFrame.FramesCount.HasValue) { var duration = TimeSpan.FromSeconds(xingFrame.FramesCount.Value * firstFrame.SamplesCount / firstFrame.Frequency); var averageBitrate = (UInt32)(contentLength / duration.TotalSeconds) * 8; file.Metadata = new Mp3Metadata(id3v2, averageBitrate, duration); } else { collectSamplesResult = await CollectSamplesAsync(stream, streamLength, null).ConfigureAwait(false); file.Metadata = new Mp3Metadata(id3v2, collectSamplesResult.Item4, collectSamplesResult.Item3); } } return(ParseResult <Mp3File> .Create(file)); } }
public static async Task <ParseResult <Mp3File> > ParseSamplesAsync(IBinaryStream stream) { var streamLength = await stream.GetLengthAsync().ConfigureAwait(false); stream.Position = 0; if (await Id3v2.IsPointingToHeaderAsync(stream).ConfigureAwait(false)) { await Id3v2.SkipAsync(stream).ConfigureAwait(false); } var collectSamplesResult = await CollectSamplesAsync(stream, streamLength, null).ConfigureAwait(false); if (collectSamplesResult.Item1.Count == 0) { return(ParseResult <Mp3File> .CreateUnknownFormat()); } var file = new Mp3File(); file.Type = collectSamplesResult.Item2; file.Samples = collectSamplesResult.Item1; return(ParseResult <Mp3File> .Create(file)); }