Esempio n. 1
0
 public static void ExportSampleSchema(SampleSchema sampleSchema, string exportFolder,
                                       Dictionary <SampleGeneratingArgs, SampleSoundGenerator> loadedSamples = null,
                                       SampleExportFormat format = SampleExportFormat.Default, SampleExportFormat mixedFormat = SampleExportFormat.Default)
 {
     foreach (var kvp in sampleSchema)
     {
         ExportMixedSample(kvp.Value, kvp.Key, exportFolder, loadedSamples, format, mixedFormat);
     }
 }
Esempio n. 2
0
        public static void ExportLoadedSamples(Dictionary <SampleGeneratingArgs, SampleSoundGenerator> loadedSamples,
                                               string exportFolder, Dictionary <SampleGeneratingArgs, string> names = null,
                                               SampleExportFormat format = SampleExportFormat.Default)
        {
            if (names == null)
            {
                names = GenerateSampleNames(loadedSamples.Keys, loadedSamples);
            }

            foreach (var sample in loadedSamples.Keys.Where(sample => SampleImporter.ValidateSampleArgs(sample, loadedSamples)))
            {
                ExportSample(sample, names[sample], exportFolder, loadedSamples, format);
            }
        }
Esempio n. 3
0
        public static void ExportCustomIndices(List <CustomIndex> customIndices, string exportFolder,
                                               Dictionary <SampleGeneratingArgs, SampleSoundGenerator> loadedSamples = null,
                                               SampleExportFormat format = SampleExportFormat.Default, SampleExportFormat mixedFormat = SampleExportFormat.Default)
        {
            foreach (CustomIndex ci in customIndices)
            {
                foreach (KeyValuePair <string, HashSet <SampleGeneratingArgs> > kvp in ci.Samples)
                {
                    if (kvp.Value.Count == 0)
                    {
                        continue;
                    }

                    string filename = ci.Index == 1 ? kvp.Key : kvp.Key + ci.Index;
                    ExportMixedSample(kvp.Value, filename, exportFolder, loadedSamples, format, mixedFormat);
                }
            }
        }
Esempio n. 4
0
        private static bool IsFormatEncodingCompatible(WaveFormatEncoding encoding, SampleExportFormat exportFormat)
        {
            switch (exportFormat)
            {
            case SampleExportFormat.WaveIeeeFloat:
                return(encoding == WaveFormatEncoding.IeeeFloat);

            case SampleExportFormat.WavePcm:
                return(encoding == WaveFormatEncoding.Pcm);

            case SampleExportFormat.OggVorbis:
                return(encoding == WaveFormatEncoding.Vorbis1 || encoding == WaveFormatEncoding.Vorbis2 ||
                       encoding == WaveFormatEncoding.Vorbis3 ||
                       encoding == WaveFormatEncoding.Vorbis1P || encoding == WaveFormatEncoding.Vorbis2P ||
                       encoding == WaveFormatEncoding.Vorbis3P);

            default:
                return(true);
            }
        }
Esempio n. 5
0
        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;
                }
            }
        }
Esempio n. 6
0
        public static bool ExportSample(SampleGeneratingArgs sampleGeneratingArgs, string name,
                                        string exportFolder, Dictionary <SampleGeneratingArgs, SampleSoundGenerator> loadedSamples = null,
                                        SampleExportFormat format = SampleExportFormat.Default)
        {
            if (sampleGeneratingArgs.CanCopyPaste && format == SampleExportFormat.Default)
            {
                var dest = Path.Combine(exportFolder, name + sampleGeneratingArgs.GetExtension());
                return(CopySample(sampleGeneratingArgs.Path, dest));
            }

            SampleSoundGenerator sampleSoundGenerator;

            if (loadedSamples != null)
            {
                if (SampleImporter.ValidateSampleArgs(sampleGeneratingArgs, loadedSamples))
                {
                    sampleSoundGenerator = loadedSamples[sampleGeneratingArgs];
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                try {
                    sampleSoundGenerator = SampleImporter.ImportSample(sampleGeneratingArgs);
                } catch (Exception ex) {
                    Console.WriteLine($@"{ex.Message} while importing sample {sampleGeneratingArgs}.");
                    return(false);
                }
            }

            var sourceEncoding = sampleSoundGenerator.Wave.WaveFormat.Encoding;

            // Either if it is the blank sample or the source file is literally what the user wants to be exported
            if (sampleSoundGenerator.BlankSample && sampleGeneratingArgs.GetExtension() == ".wav" ||
                sampleGeneratingArgs.CanCopyPaste && IsFormatEncodingCompatible(sourceEncoding, format))
            {
                var dest = Path.Combine(exportFolder, name + sampleGeneratingArgs.GetExtension());
                return(CopySample(sampleGeneratingArgs.Path, dest));
            }

            var sampleProvider = sampleSoundGenerator.GetSampleProvider();

            if ((format == SampleExportFormat.WavePcm || format == SampleExportFormat.OggVorbis) && sourceEncoding == WaveFormatEncoding.IeeeFloat)
            {
                // When the source is IEEE float and the export format is PCM or Vorbis, then clipping is possible, so we add a limiter
                sampleProvider = new SoftLimiter(sampleProvider);
            }

            switch (format)
            {
            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:
                switch (sourceEncoding)
                {
                case WaveFormatEncoding.IeeeFloat:
                    CreateWaveFile(Path.Combine(exportFolder, name + ".wav"), sampleProvider.ToWaveProvider());
                    break;

                case WaveFormatEncoding.Pcm:
                    CreateWaveFile(Path.Combine(exportFolder, name + ".wav"), sampleProvider.ToWaveProvider16());
                    break;

                case WaveFormatEncoding.Vorbis1:
                case WaveFormatEncoding.Vorbis2:
                case WaveFormatEncoding.Vorbis3:
                case WaveFormatEncoding.Vorbis1P:
                case WaveFormatEncoding.Vorbis2P:
                case WaveFormatEncoding.Vorbis3P:
                    // Vorbis files default to being exported as 16-bit PCM wave files because that's lossless
                    CreateWaveFile(Path.Combine(exportFolder, name + ".wav"), sampleProvider.ToWaveProvider16());
                    break;

                default:
                    CreateWaveFile(Path.Combine(exportFolder, name + ".wav"), sampleProvider.ToWaveProvider());
                    break;
                }

                break;
            }

            return(true);
        }
Esempio n. 7
0
        private static bool IsCopyCompatible(SampleGeneratingArgs sampleGeneratingArgs, WaveFormatEncoding waveEncoding, SampleExportFormat exportFormat)
        {
            switch (exportFormat)
            {
            case SampleExportFormat.WaveIeeeFloat:
                return(waveEncoding == WaveFormatEncoding.IeeeFloat && sampleGeneratingArgs.GetExtension() == ".wav");

            case SampleExportFormat.WavePcm:
                return(waveEncoding == WaveFormatEncoding.Pcm && sampleGeneratingArgs.GetExtension() == ".wav");

            case SampleExportFormat.OggVorbis:
                return(sampleGeneratingArgs.GetExtension() == ".ogg");

            default:
                return(true);
            }
        }
Esempio n. 8
0
        public static bool ExportSample(SampleGeneratingArgs sampleGeneratingArgs, string name,
                                        string exportFolder, Dictionary <SampleGeneratingArgs, SampleSoundGenerator> loadedSamples = null,
                                        SampleExportFormat format = SampleExportFormat.Default)
        {
            // Export as midi file with single note
            if (format == SampleExportFormat.MidiChords)
            {
                MidiExporter.SaveToFile(Path.Combine(exportFolder, name + ".mid"), new[] { sampleGeneratingArgs });
                return(true);
            }

            if (sampleGeneratingArgs.CanCopyPaste && format == SampleExportFormat.Default)
            {
                var dest = Path.Combine(exportFolder, name + sampleGeneratingArgs.GetExtension());
                return(CopySample(sampleGeneratingArgs.Path, dest));
            }

            SampleSoundGenerator sampleSoundGenerator;

            if (loadedSamples != null)
            {
                if (SampleImporter.ValidateSampleArgs(sampleGeneratingArgs, loadedSamples))
                {
                    sampleSoundGenerator = loadedSamples[sampleGeneratingArgs];
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                try {
                    sampleSoundGenerator = SampleImporter.ImportSample(sampleGeneratingArgs);
                } catch (Exception ex) {
                    Console.WriteLine($@"{ex.Message} while importing sample {sampleGeneratingArgs}.");
                    return(false);
                }
            }

            var sourceWaveEncoding = sampleSoundGenerator.Wave.WaveFormat.Encoding;

            // Either if it is the blank sample or the source file is literally what the user wants to be exported
            if (sampleSoundGenerator.BlankSample && sampleGeneratingArgs.GetExtension().ToLower() == ".wav" ||
                sampleGeneratingArgs.CanCopyPaste && IsCopyCompatible(sampleGeneratingArgs, sourceWaveEncoding, format))
            {
                var dest = Path.Combine(exportFolder, name + sampleGeneratingArgs.GetExtension());
                return(CopySample(sampleGeneratingArgs.Path, dest));
            }

            var sampleProvider = sampleSoundGenerator.GetSampleProvider();

            if ((format == SampleExportFormat.WavePcm || format == SampleExportFormat.OggVorbis) && sourceWaveEncoding == WaveFormatEncoding.IeeeFloat)
            {
                // When the source is IEEE float and the export format is PCM or Vorbis, then clipping is possible, so we add a limiter
                sampleProvider = new SoftLimiter(sampleProvider);
            }

            switch (format)
            {
            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:
                var resampled = new WdlResamplingSampleProvider(sampleProvider,
                                                                VorbisFileWriter.GetSupportedSampleRate(sampleProvider.WaveFormat.SampleRate));
                VorbisFileWriter.CreateVorbisFile(Path.Combine(exportFolder, name + ".ogg"), resampled.ToWaveProvider());
                break;

            default:
                switch (sourceWaveEncoding)
                {
                case WaveFormatEncoding.IeeeFloat:
                    CreateWaveFile(Path.Combine(exportFolder, name + ".wav"), sampleProvider.ToWaveProvider());
                    break;

                case WaveFormatEncoding.Pcm:
                    CreateWaveFile(Path.Combine(exportFolder, name + ".wav"), sampleProvider.ToWaveProvider16());
                    break;

                default:
                    CreateWaveFile(Path.Combine(exportFolder, name + ".wav"), sampleProvider.ToWaveProvider());
                    break;
                }

                break;
            }

            return(true);
        }