Ejemplo n.º 1
0
        /// <summary>
        /// Detect BPM rate of <paramref name="inFile"/> and adjust tempo
        /// setting accordingly if necessary.
        /// </summary>
        private static void DetectBpm(WavInFile inFile, RunParameters parameters)
        {
            var bpm = BpmDetect <TSampleType, TLongSampleType> .NewInstance(inFile.GetNumChannels(), inFile.GetSampleRate());

            var sampleBuffer = new TSampleType[BUFF_SIZE];

            // detect bpm rate
            Console.Error.Write("Detecting BPM rate...");
            Console.Error.Flush();

            int nChannels = inFile.GetNumChannels();

            Debug.Assert(BUFF_SIZE % nChannels == 0);

            // Process the 'inFile' in small blocks, repeat until whole file has
            // been processed
            while (inFile.Eof() == false)
            {
                // Read sample data from input file
                int num = inFile.Read(sampleBuffer, BUFF_SIZE);

                // Enter the new samples to the bpm analyzer class
                int samples = num / nChannels;
                bpm.InputSamples(sampleBuffer, samples);
            }

            // Now the whole song data has been analyzed. Read the resulting bpm.
            float bpmValue = bpm.GetBpm();

            Console.Error.WriteLine("Done!");

            // rewind the file after bpm detection
            inFile.Rewind();

            if (bpmValue > 0)
            {
                Console.Error.WriteLine("Detected BPM rate {0:0.0}\n", bpmValue);
            }
            else
            {
                Console.Error.WriteLine("Couldn't detect BPM rate.\n");
                return;
            }

            if (parameters.GoalBpm > 0)
            {
                // adjust tempo to given bpm
                parameters.TempoDelta = (parameters.GoalBpm / bpmValue - 1.0f) * 100.0f;
                Console.Error.WriteLine("The file will be converted to {0:0.0} BPM\n", parameters.GoalBpm);
            }
        }
Ejemplo n.º 2
0
        private static int Main(string[] args)
        {
            WavInFile  inFile;
            WavOutFile outFile;
            var        soundTouch = new SoundTouch <TSampleType, TLongSampleType>();

            Console.Error.WriteLine(string.Format(string.Concat(HELLOTEXT), SoundTouch <TSampleType, TLongSampleType> .VersionString));

            try
            {
                // Parse command line parameters
                var parameters = new RunParameters(args);

                // Open input & output files
                OpenFiles(out inFile, out outFile, parameters);

                if (parameters.DetectBpm)
                {
                    // detect sound BPM (and adjust processing parameters
                    //  accordingly if necessary)
                    DetectBpm(inFile, parameters);
                }

                // Setup the 'SoundTouch' object for processing the sound
                Setup(soundTouch, inFile, parameters);

                // Process the sound
                Process(soundTouch, inFile, outFile);

                if (inFile != null)
                {
                    inFile.Dispose();
                }
                if (outFile != null)
                {
                    outFile.Dispose();
                }

                Console.Error.WriteLine("Done!");
            }
            catch (Exception e)
            {
                // An exception occurred during processing, display an error message
                Console.Error.WriteLine(e.Message);
                return(-1);
            }

            return(0);
        }
Ejemplo n.º 3
0
        //private static int Main(string[] args)
        //{
        //	var parameters = new RunParameters();
        //	parameters.TempoDelta = 50;
        //	parameters.PitchDelta = 5;
        //	parameters.Speech = true;
        //	var inBytes = System.IO.File.ReadAllBytes("Test.wav");
        //	var inStream = new MemoryStream(inBytes);
        //	var outStream = new MemoryStream();
        //	Process(inStream, outStream, parameters);
        //	var outBytes = outStream.ToArray();
        //	File.WriteAllBytes("Test_Pith_Tempo_ms.wav", outBytes);
        //	return 0;
        //}


        public static void Process(Stream inStream, Stream outStream, RunParameters parameters)
        {
            var soundTouch = new SoundTouch <TSampleType, TLongSampleType>();
            // Open Input file.
            var inFile = new WavInFile(inStream);

            //var inFile = new WavInFile("Test.wav");
            int bits       = inFile.GetNumBits();
            int samplerate = inFile.GetSampleRate();
            int channels   = inFile.GetNumChannels();
            // Open output file.
            var outFile = new WavOutFile(outStream, samplerate, bits, channels);

            //var outFile = new WavOutFile("Test_Pith_Tempo.wav", samplerate, bits, channels);
            if (parameters.DetectBpm)
            {
                // detect sound BPM (and adjust processing parameters
                //  accordingly if necessary)
                DetectBpm(inFile, parameters);
            }
            // Setup the 'SoundTouch' object for processing the sound
            int sampleRate = inFile.GetSampleRate();

            soundTouch.SetSampleRate(sampleRate);
            soundTouch.SetChannels(channels);
            soundTouch.SetTempoChange(parameters.TempoDelta);
            soundTouch.SetPitchSemiTones(parameters.PitchDelta);
            soundTouch.SetRateChange(parameters.RateDelta);
            soundTouch.SetSetting(SettingId.UseQuickseek, parameters.Quick);
            soundTouch.SetSetting(SettingId.UseAntiAliasFilter, (parameters.NoAntiAlias == 1) ? 0 : 1);
            if (parameters.Speech)
            {
                // use settings for speech processing
                soundTouch.SetSetting(SettingId.SequenceDurationMs, 40);
                soundTouch.SetSetting(SettingId.SeekwindowDurationMs, 15);
                soundTouch.SetSetting(SettingId.OverlapDurationMs, 8);
            }
            // Process the sound
            Process(soundTouch, inFile, outFile);
            if (inFile != null)
            {
                inFile.Dispose();
            }
            if (outFile != null)
            {
                outFile.Dispose();
            }
        }
Ejemplo n.º 4
0
        private static void OpenFiles(out WavInFile inFile, out WavOutFile outFile, RunParameters parameters)
        {
            inFile = parameters.InFileName == "stdin" ? new WavInFile(Console.OpenStandardInput()) : new WavInFile(parameters.InFileName);

            // ... open output file with same sound parameters
            int bits       = (inFile).GetNumBits();
            int samplerate = (inFile).GetSampleRate();
            int channels   = (inFile).GetNumChannels();

            if (parameters.OutFileName != null)
            {
                outFile = parameters.OutFileName == "stdout" ? new WavOutFile(Console.OpenStandardOutput(), samplerate, bits, channels) : new WavOutFile(parameters.OutFileName, samplerate, bits, channels);
            }
            else
            {
                outFile = null;
            }
        }
Ejemplo n.º 5
0
        public static int Main(string[] args)
        {
            Console.Error.WriteLine(HELLO_TEXT, SoundTouchProcessor.VersionString);

            try
            {
                var parameters = new RunParameters(args);
                var processor  = new SoundTouchProcessor();

                OpenFiles(out var inputFile, out var outputFile, parameters);

                using (inputFile)
                    using (outputFile)
                    {
                        if (parameters.DetectBpm)
                        {
                            DetectBpm(inputFile, parameters);
                        }

                        // Setup the 'SoundTouch' object for processing the sound.
                        Setup(processor, inputFile, parameters);

                        var stopwatch = Stopwatch.StartNew();

                        // Process the sound.
                        Process(processor, inputFile, outputFile);
                        Debug.WriteLine("Duration: {0}", stopwatch.Elapsed);

                        Console.Error.WriteLine("Done!");
                    }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                return(-1);
            }

            return(0);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Sets the <c>SoundTouch</c> object up according to input file sound format & command line parameters
        /// </summary>
        private static void Setup(SoundTouch <TSampleType, TLongSampleType> pSoundTouch, WavInFile inFile, RunParameters parameters)
        {
            int sampleRate = inFile.GetSampleRate();
            int channels   = inFile.GetNumChannels();

            pSoundTouch.SetSampleRate(sampleRate);
            pSoundTouch.SetChannels(channels);

            pSoundTouch.SetTempoChange(parameters.TempoDelta);
            pSoundTouch.SetPitchSemiTones(parameters.PitchDelta);
            pSoundTouch.SetRateChange(parameters.RateDelta);

            pSoundTouch.SetSetting(SettingId.UseQuickseek, parameters.Quick);
            pSoundTouch.SetSetting(SettingId.UseAntiAliasFilter, (parameters.NoAntiAlias == 1) ? 0 : 1);

            if (parameters.Speech)
            {
                // use settings for speech processing
                pSoundTouch.SetSetting(SettingId.SequenceDurationMs, 40);
                pSoundTouch.SetSetting(SettingId.SeekwindowDurationMs, 15);
                pSoundTouch.SetSetting(SettingId.OverlapDurationMs, 8);
                Console.Error.WriteLine("Tune processing parameters for speech processing.");
            }

            // print processing information
            if (parameters.OutFileName != null)
            {
#if SOUNDTOUCH_INTEGER_SAMPLES
                Console.Error.WriteLine("Uses 16bit integer sample type in processing.\n");
#else
#if !SOUNDTOUCH_FLOAT_SAMPLES
    #error "Sampletype not defined"
#endif
                Console.Error.WriteLine("Uses 32bit floating point sample type in processing.\n");
#endif
                // print processing information only if outFileName given i.e. some processing will happen
                Console.Error.WriteLine("Processing the file with the following changes:");
                Console.Error.WriteLine("  tempo change = {0:0.00} %", parameters.TempoDelta);
                Console.Error.WriteLine("  pitch change = {0} semitones", parameters.PitchDelta);
                Console.Error.WriteLine("  rate change  = {0:0.00} %\n", parameters.RateDelta);
                Console.Error.Write("Working...");
            }
            else
            {
                // outFileName not given
                Console.Error.WriteLine("Warning: output file name missing, won't output anything.\n");
            }
            Console.Error.Flush();
        }