private async Task <long> DownloadSegment <TSource>(
            ISegment <TSource> source,
            int?targetSampleRateHz,
            AudioRecording recording,
            RemoteSegment segment,
            byte?channel,
            FileInfo destination)
        {
            Log.Debug($"Downloading media: {recording.Id}, {segment.Offsets}");
            var(stream, contentLength) = await this.mediaService.DownloadMediaWave(
                recording.Id,
                source.StartOffsetSeconds,
                source.EndOffsetSeconds,
                targetSampleRateHz,
                channel);

            Log.Trace(
                $"Downloading media: {recording.Id}, {segment.Offsets} - headers received,"
                + $" body is {contentLength?.ToString() ?? "<unknown>"} bytes, writing stream to file {destination}");

            // The output file should never exist already - if it does there's something wrong with the program
            // or a previous run of the program left behind files.
            // This restriction is similar to that found in MasterAudioUtility
            if (destination.Exists)
            {
                Log.Warn($"RemoteSource preparer is trying to create file {destination} that already exists");
            }

            // purposely use FileMode.CreateNew to ensure target file does not exist already (or else throw)
            long length;

            using (var file = File.Open(destination.FullName, FileMode.CreateNew, FileAccess.Write))
            {
                await stream.CopyToAsync(file);

                length = file.Length;
            }

            Log.Trace(
                $"Downloading media: {recording.Id}, {segment.Offsets} - file received, "
                + $"{length} bytes written to file {destination}");

            if (length == 0)
            {
                throw new RemoteSourcePreparerException(
                          "Downloaded media has a content length of zero bytes. This means media download has failed."
                          + $"{recording.Id}, {segment.Offsets} (file: {destination})");
            }

            this.recievedSizeTracker.Add(length);

            return(length);
        }
        public void TestInitialize()
        {
            this.recordingA = AudioRecordingFactory.Create(123);
            this.recordingB = AudioRecordingFactory.Create(123);
            this.recordingC = AudioRecordingFactory.Create(321);
            var offsets = 0.0.To(60);

            this.actualA = new RemoteSegment(this.recordingA, offsets);
            this.actualB = new RemoteSegment(this.recordingB, offsets);
            this.actualC = new RemoteSegment(this.recordingC, offsets);
            this.actualD = new RemoteSegment(this.recordingC, 60.0.To(120.0));
        }
        public void RemoteSegmentTestNoOffsetsProvided()
        {
            var recording = AudioRecordingFactory.Create();

            var actual = new RemoteSegment(recording);

            Assert.AreEqual(recording, actual.Source);
            Assert.AreEqual(0.0.To(recording.DurationSeconds), actual.Offsets);
            Assert.AreEqual(0.0, actual.StartOffsetSeconds);
            Assert.AreEqual(recording.DurationSeconds, actual.EndOffsetSeconds);
            Assert.AreEqual(recording.DurationSeconds, actual.SourceMetadata.Duration.TotalSeconds);
            Assert.AreEqual(recording.SampleRateHertz, actual.SourceMetadata.SampleRate);
            Assert.AreEqual(recording.Uuid, actual.SourceMetadata.Identifier);
            Assert.AreEqual(recording.RecordedDate, actual.SourceMetadata.RecordedDate);
        }
Пример #4
0
        public void SupportsNonSplittingMode()
        {
            this.preparer = new RemoteSourcePreparer(this.authenticatedApi, false);
            var segment = new RemoteSegment(this.audioRecording);

            Assert.ThrowsException <SegmentSplitException>(
                () =>
            {
                this.preparer.CalculateSegments(new[] { segment }, this.settings).ToArray();
            });

            // try again, but with a segment that is small enough
            segment = new RemoteSegment(
                this.audioRecording,
                0.0.To(this.settings.AnalysisMaxSegmentDuration.Value.TotalSeconds));

            var analysisSegments = this.preparer.CalculateSegments(new[] { segment }, this.settings).ToArray();

            var expected = new[]