예제 #1
0
        /// <summary>Load in the music files from the pack's respective Directory/Songs folder. Typically Content/Music/Wav/FolderName/Songs</summary>
        private void LoadMusicFiles()
        {
            DateTime startTime = DateTime.Now;

            DirectoryInfo songFolder = new DirectoryInfo(Path.Combine(this.ContentPack.DirectoryPath, this.MusicFolderName));

            foreach (FileInfo file in songFolder.GetFiles())
            {
                // get name
                string name = Path.GetFileNameWithoutExtension(file.Name);
                if (this.Sounds.ContainsKey(name))
                {
                    continue;
                }

                // load data
                SoundEffect effect = null;
                using (Stream waveFileStream = File.OpenRead(file.FullName))
                {
                    switch (file.Extension)
                    {
                    case ".wav":
                        effect = SoundEffect.FromStream(waveFileStream);
                        break;

                    case ".mp3":
                        using (Mp3FileReader reader = new Mp3FileReader(waveFileStream))
                            using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
                            {
                                string tempPath = Path.Combine(songFolder.FullName, $"{name}.wav");
                                StardewSymphony.ModMonitor.Log($"Converting: {tempPath}");

                                WaveFileWriter.CreateWaveFile(tempPath, pcmStream);
                                using (Stream tempStream = File.OpenRead(tempPath))
                                    effect = SoundEffect.FromStream(tempStream);
                                File.Delete(tempPath);
                            }
                        break;

                    case ".ogg":
                        // Credits: https://social.msdn.microsoft.com/Forums/vstudio/en-US/100a97af-2a1c-4b28-b464-d43611b9b5d6/converting-multichannel-ogg-to-stereo-wav-file?forum=csharpgeneral
                        using (VorbisWaveReader vorbisStream = new VorbisWaveReader(file.FullName))
                        {
                            string tempPath = Path.Combine(songFolder.FullName, $"{name}.wav");
                            StardewSymphony.DebugLog($"Converting: {tempPath}");

                            WaveFileWriter.CreateWaveFile(tempPath, vorbisStream.ToWaveProvider16());
                            using (Stream tempStream = File.OpenRead(tempPath))
                                effect = SoundEffect.FromStream(tempStream);
                            File.Delete(tempPath);
                        }
                        break;

                    default:
                        StardewSymphony.ModMonitor.Log($"Unsupported file extension {file.Extension}.", LogLevel.Warn);
                        break;
                    }
                }
                if (effect == null)
                {
                    continue;
                }

                // add sound
                SoundEffectInstance instance = effect.CreateInstance();
                this.Sounds.Add(name, instance);
                //this.SongInformation.listOfSongsWithoutTriggers.Add(name);
                this.SongInformation.songs.Add(name, new V2.SongInformation(name));
            }

            // log loading time
            if (StardewSymphony.Config.EnableDebugLog)
            {
                StardewSymphony.ModMonitor.Log($"Time to load WAV music pack {this.Name}: {startTime.Subtract(DateTime.Now)}");
            }
        }
예제 #2
0
        private static void ConvertSound(Sound sound)
        {
            if (!sound.HasStartFile && !sound.HasStartFile)
            {
                return;
            }

            FileInfo outFile = CurrentDirectory.GetDirectory("out").GetFile($"{sound.Name}.{Arguments.Format.ToString().ToLower()}");

            if (!Arguments.Overwrite && outFile.Exists)
            {
                return;
            }

            FileInfo tempWave = new FileInfo(Path.GetTempFileName());

            WaveReader reader = new WaveReader();
            AudioData  audio  = null;

            Console.WriteLine($"Processing {sound.Name}...");
            if (sound.HasStartFile && !sound.HasLoopFile)
            {
                using (VorbisWaveReader vorbisStart = new VorbisWaveReader(GameSounds.GetFile(sound.StartFileName).FullName))
                {
                    WaveFileWriter.CreateWaveFile(tempWave.FullName, vorbisStart.ToWaveProvider16());
                }
                using (FileStream stream = tempWave.OpenRead())
                {
                    audio = reader.Read(stream);
                }
            }
            else if (sound.HasStartFile && sound.HasLoopFile)
            {
                VorbisWaveReader vorbisStart = new VorbisWaveReader(GameSounds.GetFile(sound.StartFileName).FullName);
                VorbisWaveReader vorbisLoop  = new VorbisWaveReader(GameSounds.GetFile(sound.LoopFileName).FullName);
                if (vorbisStart.WaveFormat.SampleRate < vorbisLoop.WaveFormat.SampleRate)
                {
                    MediaFoundationResampler sampeler = new MediaFoundationResampler(vorbisStart, vorbisLoop.WaveFormat.SampleRate);
                    WaveFileWriter.CreateWaveFile(tempWave.FullName, sampeler.ToSampleProvider().FollowedBy(vorbisLoop).ToWaveProvider16());
                }
                else if (vorbisStart.WaveFormat.SampleRate > vorbisLoop.WaveFormat.SampleRate)
                {
                    MediaFoundationResampler sampeler = new MediaFoundationResampler(vorbisLoop, vorbisStart.WaveFormat.SampleRate);
                    WaveFileWriter.CreateWaveFile(tempWave.FullName, vorbisStart.FollowedBy(sampeler.ToSampleProvider()).ToWaveProvider16());
                }
                else
                {
                    WaveFileWriter.CreateWaveFile(tempWave.FullName, vorbisStart.FollowedBy(vorbisLoop).ToWaveProvider16());
                }
                VorbisReader dataStart = new VorbisReader(GameSounds.GetFile(sound.StartFileName).FullName);
                VorbisReader dataLoop  = new VorbisReader(GameSounds.GetFile(sound.LoopFileName).FullName);
                int          startLoop = (int)dataStart.TotalSamples;
                int          endLoop   = (int)dataStart.TotalSamples + (int)dataLoop.TotalSamples;
                vorbisStart.Dispose();
                vorbisLoop.Dispose();
                dataStart.Dispose();
                dataLoop.Dispose();
                using (FileStream stream = tempWave.OpenRead())
                {
                    audio = reader.Read(stream);
                }

                audio.SetLoop(true, startLoop, endLoop);
            }
            else if (!sound.HasStartFile && sound.HasLoopFile)
            {
                VorbisWaveReader vorbisLoop = new VorbisWaveReader(GameSounds.GetFile(sound.LoopFileName).FullName);
                WaveFileWriter.CreateWaveFile(tempWave.FullName, vorbisLoop.ToWaveProvider16());
                VorbisReader dataLoop     = new VorbisReader(GameSounds.GetFile(sound.LoopFileName).FullName);
                int          totalSamples = (int)dataLoop.TotalSamples;
                vorbisLoop.Dispose();
                dataLoop.Dispose();
                audio = reader.Read(tempWave.OpenRead());

                audio.SetLoop(true, 0, totalSamples);
            }

            if (audio != null)
            {
                BCFstmWriter writer = new BCFstmWriter(Arguments.Target);
                writer.Configuration.Codec      = Arguments.Codec;
                writer.Configuration.Endianness = Arguments.Endianness;
                using (FileStream stream = outFile.OpenWrite())
                {
                    writer.WriteToStream(audio, stream);
                }
            }
        }