C# 录音和变调

2021-04-01 14:24

阅读:687

标签:开始   ice   tor   sap   amp   sys   data   pre   rsh   

一直想研究下录音 正好有个项目有机会使用一下强大的 NAudio (https://github.com/naudio/NAudio)库

 

技术分享图片

 录音

NAudio 录音类库

public class NAudioRecorder
    {
        public WaveIn waveSource = null;
        public WaveFileWriter waveFile = null;
        private string fileName = string.Empty;

        /// 
        /// 开始录音
        /// 
        public void StartRec()
        {
            waveSource = new WaveIn();
            waveSource.WaveFormat = new WaveFormat(16000, 16, 1); // 16bit,16KHz,Mono的录音格式

            waveSource.DataAvailable += new EventHandler(waveSource_DataAvailable);
            waveSource.RecordingStopped += new EventHandler(waveSource_RecordingStopped);
            //writer = new WaveFileWriter(outputFilePath, capture.WaveFormat);
            var capture = new WasapiLoopbackCapture();
            Directory.CreateDirectory(fileName);
            var outputFilePath = Path.Combine(fileName, "recorded.wav");
            waveFile = new WaveFileWriter(outputFilePath, waveSource.WaveFormat);

            waveSource.StartRecording();
        }

        /// 
        /// 停止录音
        /// 
        public void StopRec()
        {
            waveSource.StopRecording();

            if (waveSource != null)
            {
                waveSource.Dispose();
                waveSource = null;
            }

            if (waveFile != null)
            {
                waveFile.Dispose();
                waveFile = null;
            }
        }

        /// 
        /// 录音结束后保存的文件路径
        /// 
        /// 保存wav文件的路径名
        public void SetFileName(string fileName)
        {
            this.fileName = fileName;
        }

        /// 
        /// 开始录音回调函数
        /// 
        /// 
        /// 
        private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
        {
            if (waveFile != null)
            {
                waveFile.Write(e.Buffer, 0, e.BytesRecorded);
                waveFile.Flush();
            }
        }

        /// 
        /// 录音结束回调函数
        /// 
        /// 
        /// 
        private void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
        {
            if (waveSource != null)
            {
                waveSource.Dispose();
                waveSource = null;
            }

            if (waveFile != null)
            {
                waveFile.Dispose();
                waveFile = null;
            }
        }
    }

变声

变声用到的是SoundTouch.dll 

SoundTouch wrapper 

public class SoundTouch
    {
        private IntPtr handle;

        public SoundTouch()
        {
            handle = soundtouch_createInstance();
        }


        ~SoundTouch()
        {
            soundtouch_destroyInstance(handle);
        }

        /// 
        /// Get SoundTouch version string
        /// 
        public static String GetVersionString()
        {
            // convert "char *" data to c# string
            return Marshal.PtrToStringAnsi(soundtouch_getVersionString());
        }


        /// 
        /// Returns number of processed samples currently available in SoundTouch for immediate output.
        /// 
        public uint NumSamples()
        {
            return soundtouch_numSamples(handle);
        }

        /// 
        /// Adds ‘numSamples‘ pcs of samples from the ‘samples‘ memory position into
        /// the input of the object. Notice that sample rate _has_to_ be set before
        /// calling this function, otherwise throws a runtime_error exception.
        /// 
        /// Sample buffer to input
        /// Number of sample frames in buffer. Notice
        /// that in case of multi-channel sound a single sample frame contains 
        /// data for all channels
        public void PutSamples(float[] samples, uint numSamples)
        {
            soundtouch_putSamples(handle, samples, numSamples);
        }


        /// 
        /// Sets the number of channels
        /// 
        /// 1 = mono, 2 = stereo, n = multichannel
        public void SetChannels(uint numChannels)
        {
            soundtouch_setChannels(handle, numChannels);
        }


        /// 
        /// Sets sample rate.
        /// 
        /// Samplerate, e.g. 44100
        public void SetSampleRate(uint srate)
        {
            soundtouch_setSampleRate(handle, srate);
        }


        /// 
        /// Receive processed samples from the processor.
        /// 
        /// Buffer where to copy output samples
        /// Max number of sample frames to receive
        /// 
        public uint ReceiveSamples(float[] outBuffer, uint maxSamples)
        {
            return soundtouch_receiveSamples(handle, outBuffer, maxSamples);
        }

        /// 
        /// Flushes the last samples from the processing pipeline to the output.
        /// Clears also the internal processing buffers.
        //
        /// Note: This function is meant for extracting the last samples of a sound
        /// stream. This function may introduce additional blank samples in the end
        /// of the sound stream, and thus it‘s not recommended to call this function
        /// in the middle of a sound stream.
        /// 
        public void Flush()
        {
            soundtouch_flush(handle);
        }

        /// 
        /// Clears all the samples in the object‘s output and internal processing
        /// buffers.
        /// 
        public void Clear()
        {
            soundtouch_clear(handle);
        }

        /// 
        /// Sets new tempo control value. 
        /// 
        /// Tempo setting. Normal tempo = 1.0, smaller values
        /// represent slower tempo, larger faster tempo.
        public void SetTempo(float newTempo)
        {
            soundtouch_setTempo(handle, newTempo);
        }

        /// 
        /// Sets new tempo control value as a difference in percents compared
        /// to the original tempo (-50 .. +100 %);
        /// 
        /// Tempo setting in %
        public void SetTempoChange(float newTempo)
        {
            soundtouch_setTempoChange(handle, newTempo);
        }

        /// 
        /// Sets new rate control value. 
        /// 
        /// Rate setting. Normal rate = 1.0, smaller values
        /// represent slower rate, larger faster rate.
        public void SetRate(float newRate)
        {
            soundtouch_setTempo(handle, newRate);
        }

        /// 
        /// Sets new rate control value as a difference in percents compared
        /// to the original rate (-50 .. +100 %);
        /// 
        /// Rate setting in %
        public void SetRateChange(float newRate)
        {
            soundtouch_setRateChange(handle, newRate);
        }

        /// 
        /// Sets new pitch control value. 
        /// 
        /// Pitch setting. Original pitch = 1.0, smaller values
        /// represent lower pitches, larger values higher pitch.
        public void SetPitch(float newPitch)
        {
            soundtouch_setPitch(handle, newPitch);
        }

        /// 
        /// Sets pitch change in octaves compared to the original pitch  
        /// (-1.00 .. +1.00 for +- one octave);
        /// 
        /// Pitch setting in octaves
        public void SetPitchOctaves(float newPitch)
        {
            soundtouch_setPitchOctaves(handle, newPitch);
        }

        /// 
        /// Sets pitch change in semi-tones compared to the original pitch
        /// (-12 .. +12 for +- one octave);
        /// 
        /// Pitch setting in semitones
        public void SetPitchSemiTones(float newPitch)
        {
            soundtouch_setPitchSemiTones(handle, newPitch);
        }

        /// 
        /// int16 version of soundtouch_putSamples(): This accept int16 (short) sample data
        /// and internally converts it to float format before processing
        /// 
        /// Sample input buffer.
        /// Number of sample frames in buffer. Notice
        /// that in case of multi-channel sound a single 
        /// sample frame contains data for all channels.
        public void PutSamples_i16(short[] samples, uint numSamples)
        {
            soundtouch_putSamples_i16(handle, samples, numSamples);
        }

        /// 
        /// Changes a setting controlling the processing system behaviour. See the
        /// ‘SETTING_...‘ defines for available setting ID‘s.
        /// 
        /// Setting ID number. see SETTING_... defines.
        /// 
        /// nonzero if successful, otherwise zero
        public int SetSetting(int settingId, int value)
        {
            return soundtouch_setSetting(handle, settingId, value);
        }

        /// 
        /// Reads a setting controlling the processing system behaviour. See the
        /// ‘SETTING_...‘ defines for available setting ID‘s.
        /// 
        /// Setting ID number
        /// The setting value
        public int soundtouch_getSetting(int settingId)
        {
            return soundtouch_getSetting(handle, settingId);
        }

        /// 
        /// Returns number of samples currently unprocessed in SoundTouch internal buffer
        /// 
        /// Number of sample frames
        public uint NumUnprocessedSamples()
        {
            return soundtouch_numUnprocessedSamples(handle);
        }

        /// 
        /// int16 version of soundtouch_receiveSamples(): This converts internal float samples
        /// into int16 (short) return data type
        /// 
        /// Buffer where to copy output samples.
        /// How many samples to receive at max.
        /// Number of received sample frames
        public uint soundtouch_receiveSamples_i16(short[] outBuffer, uint maxSamples)
        {
            return soundtouch_receiveSamples_i16(handle, outBuffer, maxSamples);
        }

        /// 
        /// Check if there aren‘t any samples available for outputting.
        /// 
        /// nonzero if there aren‘t any samples available for outputting
        public int IsEmpty()
        {
            return soundtouch_isEmpty(handle);
        }

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "soundtouch_getVersionId")]
        /// 
        /// Get SoundTouch library version Id
        /// 
        public static extern int GetVersionId();


        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr soundtouch_createInstance();

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_destroyInstance(IntPtr h);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr soundtouch_getVersionString();

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setRate(IntPtr h, float newRate);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setTempo(IntPtr h, float newTempo);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setRateChange(IntPtr h, float newRate);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setTempoChange(IntPtr h, float newTempo);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setPitch(IntPtr h, float newPitch);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setPitchOctaves(IntPtr h, float newPitch);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setPitchSemiTones(IntPtr h, float newPitch);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setChannels(IntPtr h, uint numChannels);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_setSampleRate(IntPtr h, uint srate);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_flush(IntPtr h);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_putSamples(IntPtr h, float[] samples, uint numSamples);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_putSamples_i16(IntPtr h, short[] samples, uint numSamples);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void soundtouch_clear(IntPtr h);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int soundtouch_setSetting(IntPtr h, int settingId, int value);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int soundtouch_getSetting(IntPtr h, int settingId);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern uint soundtouch_numUnprocessedSamples(IntPtr h);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern uint soundtouch_receiveSamples(IntPtr h, float[] outBuffer, uint maxSamples);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern uint soundtouch_receiveSamples_i16(IntPtr h, short[] outBuffer, uint maxSamples);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern uint soundtouch_numSamples(IntPtr h);

        [DllImport("SoundTouch.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int soundtouch_isEmpty(IntPtr h);
    }

  源码下载(https://files.cnblogs.com/files/leoxjy/demo.zip)

C# 录音和变调

标签:开始   ice   tor   sap   amp   sys   data   pre   rsh   

原文地址:https://www.cnblogs.com/leoxjy/p/9236646.html


评论


亲,登录后才可以留言!