public static void ExportMixedSample(IEnumerable <SampleGeneratingArgs> sampleGeneratingArgses, string name, string exportFolder, Dictionary <SampleGeneratingArgs, SampleSoundGenerator> loadedSamples = null) { var samples = new List <ISampleProvider>(); var volumes = new List <double>(); int soundsAdded = 0; if (loadedSamples != null) { foreach (var sample in from args in sampleGeneratingArgses where SampleImporter.ValidateSampleArgs(args, loadedSamples) select loadedSamples[args]) { samples.Add(sample.GetSampleProvider()); volumes.Add(Math.Abs(sample.VolumeCorrection - -1) > Precision.DOUBLE_EPSILON ? sample.VolumeCorrection : 1f); soundsAdded++; } } else { foreach (SampleGeneratingArgs args in sampleGeneratingArgses) { try { var sample = SampleImporter.ImportSample(args); samples.Add(sample.GetSampleProvider()); volumes.Add(Math.Abs(sample.VolumeCorrection - -1) > Precision.DOUBLE_EPSILON ? sample.VolumeCorrection : 1f); soundsAdded++; } catch (Exception ex) { Console.WriteLine($@"{ex.Message} while importing sample {args}."); } } } if (soundsAdded == 0) { return; } int maxSampleRate = samples.Max(o => o.WaveFormat.SampleRate); int maxChannels = samples.Max(o => o.WaveFormat.Channels); IEnumerable <ISampleProvider> sameFormatSamples = samples.Select(o => (ISampleProvider) new WdlResamplingSampleProvider(SampleImporter.SetChannels(o, maxChannels), maxSampleRate)); ISampleProvider result = new MixingSampleProvider(sameFormatSamples); if (soundsAdded > 1) { result = new VolumeSampleProvider(result) { Volume = (float)(1 / Math.Sqrt(soundsAdded * volumes.Average())) }; result = new SimpleCompressorEffect(result) { Threshold = 16, Ratio = 6, Attack = 0.1, Release = 0.1, Enabled = true, MakeUpGain = 15 * Math.Log10(Math.Sqrt(soundsAdded * volumes.Average())) }; } // TODO: Allow mp3, ogg and aif export. string filename = name + ".wav"; CreateWaveFile(Path.Combine(exportFolder, filename), result.ToWaveProvider16()); }
public static void ExportMixedSample(IEnumerable <SampleGeneratingArgs> sampleGeneratingArgses, string name, string exportFolder, Dictionary <SampleGeneratingArgs, SampleSoundGenerator> loadedSamples = null, SampleExportFormat format = SampleExportFormat.Default, SampleExportFormat mixedFormat = SampleExportFormat.Default) { // Try loading all the valid samples var validLoadedSamples = new Dictionary <SampleGeneratingArgs, SampleSoundGenerator>(); if (loadedSamples != null) { foreach (var args in sampleGeneratingArgses) { if (!SampleImporter.ValidateSampleArgs(args, loadedSamples)) { continue; } var sample = loadedSamples[args]; validLoadedSamples.Add(args, sample); } } else { // Import each sample individually foreach (SampleGeneratingArgs args in sampleGeneratingArgses) { try { var sample = SampleImporter.ImportSample(args); validLoadedSamples.Add(args, sample); } catch (Exception ex) { Console.WriteLine($@"{ex.Message} while importing sample {args}."); } } } if (validLoadedSamples.Count == 0) { return; } // If all the valid samples are blank samples, then also export only a single blank sample if (validLoadedSamples.Count == 1 || validLoadedSamples.All(o => o.Value.BlankSample)) { // It has only one valid sample, so we can just export it with the single sample export ExportSample(validLoadedSamples.Keys.First(), name, exportFolder, loadedSamples, format); } else if (validLoadedSamples.Count > 1) { // Synchronize the sample rate and channels for all samples and get the sample providers int maxSampleRate = validLoadedSamples.Values.Max(o => o.Wave.WaveFormat.SampleRate); int maxChannels = validLoadedSamples.Values.Max(o => o.Wave.WaveFormat.Channels); IEnumerable <ISampleProvider> sameFormatSamples = validLoadedSamples.Select(o => (ISampleProvider) new WdlResamplingSampleProvider(SampleImporter.SetChannels(o.Value.GetSampleProvider(), maxChannels), maxSampleRate)); ISampleProvider sampleProvider = new MixingSampleProvider(sameFormatSamples); // If the input is Ieee float or you are mixing multiple samples, then clipping is possible, // so you can either export as IEEE float or use a compressor and export as 16-bit PCM (half filesize) or Vorbis (even smaller filesize) // If the input is only The Blank Sample then it should export The Blank Sample if (mixedFormat == SampleExportFormat.WavePcm || mixedFormat == SampleExportFormat.OggVorbis) { // When the sample is mixed and the export format is PCM or Vorbis, then clipping is possible, so we add a limiter sampleProvider = new SoftLimiter(sampleProvider); } switch (mixedFormat) { case SampleExportFormat.WaveIeeeFloat: CreateWaveFile(Path.Combine(exportFolder, name + ".wav"), sampleProvider.ToWaveProvider()); break; case SampleExportFormat.WavePcm: CreateWaveFile(Path.Combine(exportFolder, name + ".wav"), sampleProvider.ToWaveProvider16()); break; case SampleExportFormat.OggVorbis: VorbisFileWriter.CreateVorbisFile(Path.Combine(exportFolder, name + ".ogg"), sampleProvider.ToWaveProvider()); break; default: CreateWaveFile(Path.Combine(exportFolder, name + ".wav"), sampleProvider.ToWaveProvider()); break; } } }
public static void ExportCustomIndices(List<CustomIndex> customIndices, string exportFolder, Dictionary<SampleGeneratingArgs, SampleSoundGenerator> loadedSamples=null) { foreach (CustomIndex ci in customIndices) { foreach (KeyValuePair<string, HashSet<SampleGeneratingArgs>> kvp in ci.Samples) { if (kvp.Value.Count == 0) { continue; } var samples = new List<ISampleProvider>(); var volumes = new List<double>(); int soundsAdded = 0; if (loadedSamples != null) { foreach (SampleGeneratingArgs args in kvp.Value) { if (SampleImporter.ValidateSampleArgs(args, loadedSamples)) { var sample = loadedSamples[args]; samples.Add(sample.GetSampleProvider()); volumes.Add(sample.VolumeCorrection != -1 ? sample.VolumeCorrection : 1f); soundsAdded++; } } } else { foreach (SampleGeneratingArgs args in kvp.Value) { try { var sample = SampleImporter.ImportSample(args); samples.Add(sample.GetSampleProvider()); volumes.Add(sample.VolumeCorrection != -1 ? sample.VolumeCorrection : 1f); soundsAdded++; } catch (Exception) { } } } if (soundsAdded == 0) { continue; } int maxSampleRate = samples.Max(o => o.WaveFormat.SampleRate); int maxChannels = samples.Max(o => o.WaveFormat.Channels); IEnumerable<ISampleProvider> sameFormatSamples = samples.Select(o => (ISampleProvider)new WdlResamplingSampleProvider(SampleImporter.SetChannels(o, maxChannels), maxSampleRate)); ISampleProvider result = new MixingSampleProvider(sameFormatSamples); if (soundsAdded > 1) { result = new VolumeSampleProvider(result) { Volume = (float)(1 / Math.Sqrt(soundsAdded * volumes.Average())) }; result = new SimpleCompressorEffect(result) { Threshold = 16, Ratio = 6, Attack = 0.1, Release = 0.1, Enabled = true, MakeUpGain = 15 * Math.Log10(Math.Sqrt(soundsAdded * volumes.Average())) }; } // TODO: Allow mp3, ogg and aif export. string filename = ci.Index == 1 ? kvp.Key + ".wav" : kvp.Key + ci.Index + ".wav"; CreateWaveFile(Path.Combine(exportFolder, filename), result.ToWaveProvider16()); } } }