/// <summary>
        /// Builds the content for the source audio.
        /// </summary>
        /// <param name="input">The audio content to build.</param>
        /// <param name="context">Context for the specified processor.</param>
        /// <returns>The built audio.</returns>
        public override SoundEffectContent Process(
            AudioContent input, ContentProcessorContext context)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var profile      = AudioProfile.ForPlatform(context.TargetPlatform);
            var finalQuality = profile.ConvertAudio(context.TargetPlatform, Quality, input);

            if (Quality != finalQuality)
            {
                context.Logger.LogMessage(
                    "Failed to convert using \"{0}\" quality, used \"{1}\" quality",
                    Quality,
                    finalQuality);
            }

            return(new SoundEffectContent(
                       input.Format.NativeWaveFormat,
                       input.Data,
                       input.DataLength,
                       input.LoopStart,
                       input.LoopLength,
                       input.Duration));
        }
        /// <summary>
        /// Builds the content for the source audio.
        /// </summary>
        /// <param name="input">The audio content to build.</param>
        /// <param name="context">Context for the specified processor.</param>
        /// <returns>The built audio.</returns>
        public override SongContent Process(AudioContent input, ContentProcessorContext context)
        {
            // The xnb name is the basis for the final song filename.
            string songInputFile = context.OutputFilename;

            // Convert and write out the song media file.
            var profile      = AudioProfile.ForPlatform(context.TargetPlatform);
            var finalQuality = profile.ConvertStreamingAudio(
                context.TargetPlatform,
                Quality,
                input,
                songInputFile,
                out string songOutFile);

            // Let the pipeline know about the song file so it can clean things up.
            context.AddOutputFile(songOutFile);

            if (Quality != finalQuality)
            {
                context.Logger.LogMessage(
                    "Failed to convert using \"{0}\" quality, used \"{1}\" quality",
                    Quality, finalQuality);
            }

            // Return the XNB song content.
            string dirPath  = Path.GetDirectoryName(context.OutputFilename) + Path.DirectorySeparatorChar;
            string filePath = PathHelper.GetRelativePath(dirPath, songOutFile);

            return(new SongContent(filePath, input.Duration));
        }
Exemple #3
0
        public override SongContent Process(AudioContent input, ContentProcessorContext context)
        {
            var outputFilename      = Path.ChangeExtension(context.OutputFilename, "ogg");
            var songContentFilename =
                PathHelper.GetRelativePath(Path.GetDirectoryName(context.OutputFilename) + Path.DirectorySeparatorChar,
                                           outputFilename);

            context.AddOutputFile(outputFilename);

            // Use the ogg-file as-is
            if (Path.GetExtension(input.FileName) == "ogg")
            {
                File.Copy(input.FileName, outputFilename);

                return((SongContent)_songContentConstructor.Invoke(new object[]
                {
                    songContentFilename,
                    input.Duration
                }));
            }

            // Prepare some useful paths and checks
            var hashFile      = Path.ChangeExtension(input.FileName, "hash");
            var oggFile       = Path.ChangeExtension(input.FileName, "ogg");
            var oggFileExists = File.Exists(oggFile);

            // Compare a previous hash, if there is one.
            var    currentHash  = CalculateSHA1(input.FileName);
            string previousHash = null;

            if (File.Exists(hashFile) && oggFileExists)
            {
                previousHash = File.ReadAllText(hashFile);
            }
            else
            {
                File.WriteAllText(hashFile, currentHash);
            }

            // Determine if we can re-use a previously generated ogg-file
            if (oggFileExists && previousHash == currentHash)
            {
                File.Copy(oggFile, outputFilename);
            }
            else
            {
                var conversionQuality = AudioProfile.ForPlatform(TargetPlatform.DesktopGL)
                                        .ConvertStreamingAudio(TargetPlatform.DesktopGL, Quality, input, ref outputFilename);
                if (Quality != conversionQuality)
                {
                    context.Logger.LogMessage("Failed to convert using \"{0}\" quality, used \"{1}\" quality", Quality,
                                              conversionQuality);
                }

                if (oggFileExists)
                {
                    File.Delete(oggFile);
                }

                File.Copy(outputFilename, oggFile);
            }

            return((SongContent)_songContentConstructor.Invoke(new object[]
            {
                songContentFilename,
                input.Duration
            }));
        }