Beispiel #1
0
        /// <summary>
        /// Resample the source waveform file to 16k Hz waveform file.
        /// </summary>
        /// <param name="sourceFile">Location of source waveform file.</param>
        /// <param name="targetFile">Location of target waveform file.</param>
        /// <param name="targetSamplesPerSecond">Samples per second of the target waveform file.</param>
        public static void Resample(string sourceFile, string targetFile, int targetSamplesPerSecond)
        {
            if (string.IsNullOrEmpty(sourceFile))
            {
                throw new ArgumentNullException("sourceFile");
            }

            if (string.IsNullOrEmpty(targetFile))
            {
                throw new ArgumentNullException("targetFile");
            }

            WaveFormat format = WaveFile.ReadFormat(sourceFile);
            if (format.SamplesPerSecond < targetSamplesPerSecond)
            {
                throw new NotSupportedException(Helper.NeutralFormat(
                    "Resampling tool will introduce obvious aliasing " +
                    "noise when upsampling from [{0}] to [[1}], refer to bug #12628",
                    format.SamplesPerSecond, targetSamplesPerSecond));
            }

            WaveFile waveFile = new WaveFile();
            waveFile.Load(sourceFile);

            Resample(waveFile, targetSamplesPerSecond);

            Helper.EnsureFolderExistForFile(targetFile);
            waveFile.Save(targetFile);
        }
        /// <summary>
        /// Affix waveform file to certain waveform file.
        /// </summary>
        /// <param name="sourceWaveDir">Source waveform file directory.</param>
        /// <param name="targetWaveDir">Target waveform file directory.</param>
        /// <param name="affixingFile">Affixing waveform file.</param>
        private static void AffixWaveFiles(string sourceWaveDir,
            string targetWaveDir, WaveFile affixingFile)
        {
            Dictionary<string, string> srcMap =
                Microsoft.Tts.Offline.FileListMap.Build(sourceWaveDir, ".wav");
            foreach (string id in srcMap.Keys)
            {
                string dstFilePath = Path.Combine(targetWaveDir, srcMap[id] + ".wav");
                if (File.Exists(dstFilePath))
                {
                    continue;
                }

                string srcFilePath = Path.Combine(sourceWaveDir, srcMap[id] + ".wav");

                Helper.EnsureFolderExistForFile(dstFilePath);

                WaveFile tgtWf = new WaveFile();
                WaveFile srcWf = new WaveFile();
                srcWf.Load(srcFilePath);

                tgtWf.Append(affixingFile);
                tgtWf.Append(srcWf);
                tgtWf.Append(affixingFile);

                tgtWf.Save(dstFilePath);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Write a Single[] to a WAV file. Mostly for debugging purposes, this
        /// Routine generates a WAV file whose duration is the same as
        /// That of the original waveform.
        /// </summary>
        /// <param name="filePath">Target file to save.</param>
        /// <param name="outWave">Waveform samples to save.</param>
        /// <param name="samplesPerSecond">Samples per second.</param>
        public static void WriteWaveFile(string filePath, float[] outWave, int samplesPerSecond)
        {
            short[] waveData = ArrayHelper.ToInt16<float>(outWave);

            WaveFormat waveFormat = new WaveFormat();
            waveFormat.Channels = 1;
            waveFormat.BlockAlign = 2;
            waveFormat.BitsPerSample = 16;
            waveFormat.ExtSize = 0;
            waveFormat.FormatTag = WaveFormatTag.Pcm;
            waveFormat.SamplesPerSecond = samplesPerSecond;
            waveFormat.AverageBytesPerSecond = checked(samplesPerSecond * 2);

            WaveFile waveFile = new WaveFile();
            waveFile.Format = waveFormat;

            RiffChunk waveDataChunk = waveFile.Riff.GetChunk(Riff.IdData);
            byte[] byteData = ArrayHelper.BinaryConvertArray(waveData);
            waveDataChunk.SetData(byteData);
            waveDataChunk.Size = waveDataChunk.GetData().Length;

            waveFile.Save(filePath);
        }