예제 #1
0
        public void GetFileNameTestStartAndEndOffsetsNew()
        {
            var actual = AudioFilePreparer.GetFileName("original.mp3", MediaTypes.MediaTypeWav, 72.Seconds(), 600.Seconds());

            Assert.AreEqual("original_72-600.wav", actual);
        }
        /// <summary>
        /// Prepare an audio file. This will be a single segment of a larger audio file, modified based on the analysisSettings.
        /// </summary>
        /// <param name="outputDirectory">
        ///     The analysis Base Directory.
        /// </param>
        /// <param name="source">
        ///     The source audio file.
        /// </param>
        /// <param name="outputMediaType">
        ///     The output Media Type.
        /// </param>
        /// <param name="targetSampleRateHz">
        ///     The target Sample Rate Hz.
        /// </param>
        /// <returns>
        /// The prepared file. The returned FileSegment will have the targetFile and OriginalFileDuration set -
        /// these are the path to the segmented file and the duration of the segmented file.
        /// The start and end offsets will not be set.
        /// </returns>
        public async Task <FileSegment> PrepareFile <TSource>(
            DirectoryInfo outputDirectory,
            ISegment <TSource> source,
            string outputMediaType,
            int?targetSampleRateHz,
            DirectoryInfo temporaryFilesDirectory,
            int[] channelSelection,
            bool?mixDownToMono)
        {
            if (!(source is RemoteSegment segment))
            {
                throw new NotImplementedException(
                          $"{nameof(RemoteSourcePreparer)} only supports preparing files with {nameof(RemoteSegment)} sources");
            }

            if (channelSelection != null && channelSelection.Length > 1)
            {
                throw new NotSupportedException(
                          $"{nameof(RemoteSourcePreparer)} does not support multi channel selection");
            }

            var recording  = segment.Source;
            var identifier = segment.SourceMetadata.Identifier;
            var filename   = AudioFilePreparer.GetFileName(
                identifier,
                outputMediaType,
                source.StartOffsetSeconds.Seconds(),
                source.EndOffsetSeconds.Seconds());

            var destination = outputDirectory.CombineFile(filename);

            // channel values:
            // null - select all channels
            // 0 - mixdown all channels
            // n - select nth channel
            byte?channel = (mixDownToMono ?? false)
                ? 0
                : (channelSelection == null ? (byte?)null : (byte)channelSelection[0]);

            int attemptCount = 0;

            while (attemptCount < RetryDelay.Length)
            {
                if (attemptCount > 0)
                {
                    Log.Debug(
                        $"Attempting to download media: {recording.Id}, {segment.Offsets} - attempt {attemptCount}");

                    destination.TryDelete();
                }

                attemptCount++;

                try
                {
                    var length = await this.DownloadSegment(
                        source,
                        targetSampleRateHz,
                        recording,
                        segment,
                        channel,
                        destination);

                    // exit retry loop if successful
                    break;
                }
                catch (IOException ioex) when(ioex.InnerException is SocketException)
                {
                    Log.Warn($"Media download failed {recording.Id}, {segment.Offsets}", ioex);
                }
                catch (RemoteSourcePreparerException rspex) when(attemptCount < RetryDelay.Length)
                {
                    Log.Warn($"Media download failed {recording.Id}, {segment.Offsets}", rspex);
                }

                // back off and then try again
                await Task.Delay(RetryDelay[attemptCount].Seconds());
            }

            // finally inspect the bit of audio we downloaded, extract the metadata, and return a file segment
            var preparedFile = new FileSegment(destination, segment.Source.RecordedDate.AddSeconds(segment.StartOffsetSeconds));

            // do some sanity checks
            var expectedDuration = segment.Offsets.Size().Seconds();
            var durationDelta    = expectedDuration - preparedFile.TargetFileDuration.Value;

            if (durationDelta > 1.0.Seconds())
            {
                Log.Warn(
                    $"Downloaded media ({recording.Id}, {segment.Offsets}) did not have expected duration."
                    + $" Expected: {expectedDuration}, Actual: {preparedFile.TargetFileDuration}");
            }

            return(preparedFile);
        }
예제 #3
0
        public void GetFileNameTestNonRoundedOffsetNew()
        {
            var actual = AudioFilePreparer.GetFileName("original.mp3", MediaTypes.MediaTypeWav, 90.Seconds(), null);

            Assert.AreEqual("original_90.wav", actual);
        }
예제 #4
0
        public void GetFileNameTestRealFractionRoundedOffsetCappedAtThreePlacesNew()
        {
            var actual = AudioFilePreparer.GetFileName("original.mp3", MediaTypes.MediaTypeWav, 67.6666.Seconds(), null);

            Assert.AreEqual("original_67.667.wav", actual);
        }
예제 #5
0
        public void GetFileNameTestNullOffsetsNew()
        {
            var actual = AudioFilePreparer.GetFileName("original.mp3", MediaTypes.MediaTypeWav, null, null);

            Assert.AreEqual("original_0.wav", actual);
        }
예제 #6
0
        public void GetFileNameTestStartAndEndOffsetsRealFractionCappedAtSixPlaces()
        {
            var actual = AudioFilePreparer.GetFileName("original.mp3", MediaTypes.MediaTypeWav, 73.123.Seconds(), 127.Seconds(), true);

            Assert.AreEqual("original_1.218717-2.116667min.wav", actual);
        }
예제 #7
0
        public void GetFileNameTestRealFractionRoundedOffsetCappedAtSixPlaces()
        {
            var actual = AudioFilePreparer.GetFileName("original.mp3", MediaTypes.MediaTypeWav, 67.Seconds(), null, true);

            Assert.AreEqual("original_1.116667min.wav", actual);
        }
예제 #8
0
        public void GetFileNameTestStartOffset()
        {
            var actual = AudioFilePreparer.GetFileName("original.mp3", MediaTypes.MediaTypeWav, 3660.Seconds(), null, true);

            Assert.AreEqual("original_61min.wav", actual);
        }
예제 #9
0
        public void GetFileNameTestStartAndEndOffsetsRealFractionCappedAtThreePlacesNew()
        {
            var actual = AudioFilePreparer.GetFileName("original.mp3", MediaTypes.MediaTypeWav, 73.12399.Seconds(), 127.33333333.Seconds());

            Assert.AreEqual("original_73.124-127.333.wav", actual);
        }