Пример #1
0
        /// <summary>
        /// Perform channel swapping while exporting to a file.
        /// </summary>
        void ExportProcess(string path)
        {
            int channels = reader.ChannelCount;

            int[] targetIndexes = new int[channels];
            for (int i = 0; i < channels; ++i)
            {
                targetIndexes[i] = -1;
                for (int j = 0; j < channels; ++j)
                {
                    if ((ReferenceChannel)sourceChannels.Items[i] == (ReferenceChannel)targetChannels.Items[j])
                    {
                        targetIndexes[i] = j;
                    }
                }
            }

            using RIFFWaveWriter writer = new(path, channels, reader.Length, reader.SampleRate, reader.Bits);
            reader.Reset();
            writer.WriteHeader();
            long position = 0,
                 end      = channels * reader.Length;

            float[] source = new float[channels * reader.SampleRate],
            target = new float[source.Length];

            while (position < end)
            {
                long stepSize = Math.Min(source.Length, end - position);
                reader.ReadBlock(source, 0, stepSize);
                Array.Clear(target, 0, (int)stepSize);
                for (int ch = 0; ch < channels; ++ch)
                {
                    int targetIndex = targetIndexes[ch];
                    if (targetIndex == -1)
                    {
                        continue;
                    }
                    for (int sample = ch, pair = targetIndex; sample < source.Length; sample += channels, pair += channels)
                    {
                        target[pair] = source[sample];
                    }
                }
                writer.WriteBlock(target, 0, stepSize);
                position += stepSize;

                double progress = position / (double)end;
                process.UpdateStatusLazy($"Exporting... ({progress:0.00%})");
                process.UpdateProgressBar(progress);
            }

            process.UpdateStatus("Finished!");
            process.UpdateProgressBar(1);
        }
        float[] Read(RIFFWaveReader reader, string instrument, double progressGap)
        {
            const long sampleBlock = 8192;
            double     progressSrc = task.Progress;

            float[] read = new float[reader.Length * reader.ChannelCount];
            for (long sample = 0; sample < read.LongLength;)
            {
                double progress = sample / (double)read.LongLength;
                reader.ReadBlock(read, sample, Math.Min(sample += sampleBlock, read.LongLength));
                task.UpdateProgressBar(progressSrc + progressGap * progress);
                task.UpdateStatusLazy(string.Format("Reading {0} ({1})...", instrument, progress.ToString("0.00%")));
            }
            task.UpdateProgressBar(progressSrc + progressGap);
            task.UpdateStatus(string.Format("Reading {0} (100%)...", instrument));
            reader.Dispose();
            return(read);
        }