private void AudioExport(string filename, int format)
        {
            var extension = format == AudioFormatType.Mp3 ? "mp3" : (format == AudioFormatType.Vorbis ? "ogg" : "wav");

            if (!ValidateExtension(filename, "." + extension))
            {
                return;
            }

            var songIndex  = ParseOption("export-song", 0);
            var sampleRate = ParseOption($"{extension}-export-rate", 44100);
            var loopCount  = ParseOption($"{extension}-export-loop", 1);
            var duration   = ParseOption($"{extension}-export-duration", 0);
            var mask       = ParseOption($"{extension}-export-channels", 0xff, true);
            var separate   = HasOption($"{extension}-export-separate-channels");
            var intro      = HasOption($"{extension}-export-separate-intro");
            var bitrate    = ParseOption($"{extension}-export-bitrate", 192);
            var song       = GetProjectSong(songIndex);

            if (duration > 0)
            {
                loopCount = -1;
            }
            else
            {
                loopCount = Math.Max(1, loopCount);
            }

            if (song != null)
            {
                AudioExportUtils.Save(song, filename, sampleRate, loopCount, duration, mask, separate, intro, false, null,
                                      (samples, samplesChannels, fn) =>
                {
                    switch (format)
                    {
                    case AudioFormatType.Mp3:
                        Mp3File.Save(samples, fn, sampleRate, bitrate, samplesChannels);
                        break;

                    case AudioFormatType.Wav:
                        WaveFile.Save(samples, fn, sampleRate, samplesChannels);
                        break;

                    case AudioFormatType.Vorbis:
                        VorbisFile.Save(samples, fn, sampleRate, bitrate, samplesChannels);
                        break;
                    }
                });
            }
        }
示例#2
0
        private void ExportWavMp3()
        {
            var props  = dialog.GetPropertyPage((int)ExportFormat.WavMp3);
            var format = props.GetSelectedIndex(1);

            Action <string> ExportWavMp3Action = (filename) =>
            {
                if (filename != null)
                {
                    var songName      = props.GetPropertyValue <string>(0);
                    var sampleRate    = Convert.ToInt32(props.GetPropertyValue <string>(2), CultureInfo.InvariantCulture);
                    var bitrate       = Convert.ToInt32(props.GetPropertyValue <string>(3), CultureInfo.InvariantCulture);
                    var loopCount     = props.GetPropertyValue <string>(4) != "Duration" ? props.GetPropertyValue <int>(5) : -1;
                    var duration      = props.GetPropertyValue <string>(4) == "Duration" ? props.GetPropertyValue <int>(6) : -1;
                    var separateFiles = props.GetPropertyValue <bool>(7);
                    var separateIntro = props.GetPropertyValue <bool>(8);
                    var stereo        = props.GetPropertyValue <bool>(9) && !separateFiles;
                    var song          = project.GetSong(songName);

                    var channelCount = project.GetActiveChannelCount();
                    var channelMask  = 0;
                    var pan          = (float[])null;

                    if (PlatformUtils.IsDesktop)
                    {
                        pan = new float[channelCount];

                        for (int i = 0; i < channelCount; i++)
                        {
                            if (props.GetPropertyValue <bool>(10, i, 0))
                            {
                                channelMask |= (1 << i);
                            }

                            pan[i] = props.GetPropertyValue <int>(10, i, 2) / 100.0f;
                        }
                    }
                    else
                    {
                        var selectedChannels = props.GetPropertyValue <bool[]>(10);
                        for (int i = 0; i < channelCount; i++)
                        {
                            if (selectedChannels[i])
                            {
                                channelMask |= (1 << i);
                            }
                        }
                    }

                    AudioExportUtils.Save(song, filename, sampleRate, loopCount, duration, channelMask, separateFiles, separateIntro, stereo, pan,
                                          (samples, samplesChannels, fn) =>
                    {
                        switch (format)
                        {
                        case AudioFormatType.Mp3:
                            Mp3File.Save(samples, fn, sampleRate, bitrate, samplesChannels);
                            break;

                        case AudioFormatType.Wav:
                            WaveFile.Save(samples, fn, sampleRate, samplesChannels);
                            break;

                        case AudioFormatType.Vorbis:
                            VorbisFile.Save(samples, fn, sampleRate, bitrate, samplesChannels);
                            break;
                        }
                    });

                    lastExportFilename = filename;
                }
            };

            if (PlatformUtils.IsMobile)
            {
                var songName = props.GetPropertyValue <string>(0);
                PlatformUtils.StartMobileSaveFileOperationAsync(AudioFormatType.MimeTypes[format], $"{songName}", (f) =>
                {
                    ExportWavMp3Action(f);
                    PlatformUtils.FinishMobileSaveFileOperationAsync(true, () => { PlatformUtils.ShowToast("Audio Export Successful!"); });
                });
            }
            else
            {
                var filename = (string)null;

                if (lastExportFilename != null)
                {
                    filename = lastExportFilename;
                }
                else
                {
                    filename = PlatformUtils.ShowSaveFileDialog(
                        $"Export {AudioFormatType.Names[format]} File",
                        $"{AudioFormatType.Names[format]} Audio File (*.{AudioFormatType.Extensions[format]})|*.{AudioFormatType.Extensions[format]}",
                        ref Settings.LastExportFolder);
                }

                ExportWavMp3Action(filename);
            }
        }