C # recording and pitch modulation

Source: Internet
Author: User

Always wanted to study the recording. There's a project that has a chance to use the powerful Naudio (Https://github.com/naudio/NAudio) Library

Recording

Naudio Recording Class Library

public class Naudiorecorder {public WaveIn wavesource = null;        Public Wavefilewriter wavefile = null; private string fileName = String.        Empty; <summary>///Start recording///</summary> public void Startrec () {Wavesour            CE = new WaveIn (); Wavesource.waveformat = new Waveformat (16000, 16, 1); 16bit,16khz,mono's recording format wavesource.dataavailable + = new Eventhandler<waveineventargs> (WaveSource_DataAva            ilable);            wavesource.recordingstopped + = new eventhandler<stoppedeventargs> (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 ();   }///<summary>///Stop recording///</summary> public void Stoprec () {            Wavesource.stoprecording ();                if (Wavesource! = null) {wavesource.dispose ();            Wavesource = null;                } if (wavefile! = null) {wavefile.dispose ();            Wavefile = null; }}///<summary>///The file path saved after recording ends///</summary>//<param name= "Filen Ame "> Save path name for WAV file </param> public void Setfilename (string fileName) {this.filename = File        Name; }///<summary>//Start recording callback function///</summary>//<param name= "Sender" ></pa ram>//<param name= "E" ></param> private void Wavesource_dataavailable (object sender, WaveIn EventArgs e) {if (wavefile! = null) {WavefilE.write (e.buffer, 0, e.bytesrecorded);            Wavefile.flush (); }}///<summary>///audio End callback function///</summary>//<param name= "Sender" &G t;</param>//<param name= "E" ></param> private void wavesource_recordingstopped (object se                NDEr, Stoppedeventargs e) {if (Wavesource! = null) {wavesource.dispose ();            Wavesource = null;                } if (wavefile! = null) {wavefile.dispose ();            Wavefile = null; }        }    }
Voice Changer

The voice changer used to be SoundTouch.dll.

SoundTouch Wrapper

public class SoundTouch {private INTPTR handle;        Public SoundTouch () {handle = Soundtouch_createinstance ();        } ~soundtouch () {soundtouch_destroyinstance (handle); }//<summary>//Get SoundTouch Version string///</summary> public static STR ing getversionstring () {//convert "char *" Data to C # string return Marshal.ptrtostringansi        (Soundtouch_getversionstring ()); }//<summary>//Returns number of processed samples currently available in SoundTouch for Immediat        E output.        </summary> public UINT NumSamples () {return soundtouch_numsamples (handle); }///<summary>//Adds ' NumSamples ' pcs of samples from the ' samples ' memory position into/ /The input of the object. Notice that the sample rate _has_to_ is set before//calling this FuNction, otherwise throws a Runtime_error exception. </summary>//<param name= "samples" >sample buffer to input</param>//<param name = "NumSamples" >number of sample frames in buffer. Notice//, multi-channel sound a single sample frame contains//data for all channels< /param> public void Putsamples (float[] samples, uint numsamples) {soundtouch_putsamples (Handl        E, samples, numsamples); }//<summary>//sets the number of channels//</summary>//<param name=        "Numchannels" >1 = Mono, 2 = stereo, n = multichannel</param> public void setchannels (UINT numchannels)        {soundtouch_setchannels (handle, numchannels);        }///<summary>//sets sample rate. </summary>//<param name= "srate" >samplerate, e.g. 44100</param> public void Setsamplerate (UINT srate) {soundtouch_setsamplerate (handle, srate);        }//<summary>//Receive processed samples from the processor.  </summary>//<param name= "Outbuffer" >buffer where to copy output samples</param>// <param name= "Maxsamples" >max number of sample frames to receive</param>//<returns></return s> public UINT Receivesamples (float[] outbuffer, uint maxsamples) {return soundtouch_receives        Amples (handle, Outbuffer, maxsamples);        }///<summary>//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 could introduce additional blank samples in the end///of the sound stream, and thus it's not recommen DEd to call this function///In the middle of a sound stream.        </summary> public void Flush () {Soundtouch_flush (handle); }//<summary>//Clears all the samples in the object ' s output and internal processing//b        Uffers.        </summary> public void Clear () {soundtouch_clear (handle);         }//<summary>//sets new tempo control value. </summary>//<param name= "Newtempo" >tempo setting. Normal Tempo = 1.0, smaller values//represent slower tempo, larger faster tempo.</param> public voi        D settempo (float newtempo) {soundtouch_settempo (handle, Newtempo); }///<summary>//sets new tempo control value as a difference in percents compared//to TH        E Original tempo (-50.. +100); </summary>//<param name= "NewteMPO ">tempo setting in%</param> public void Settempochange (float newtempo) {Soundtouch_        Settempochange (handle, Newtempo);         }//<summary>//sets new rate control value. </summary>//<param name= "newrate" >rate setting. Normal Rate = 1.0, smaller values//represent slower rate, larger faster rate.</param> public void S        Etrate (float newrate) {soundtouch_settempo (handle, newrate);  }//<summary>//sets new rate control value as a difference in percents compared//to the        Original Rate (-50: +100); </summary>//<param name= "newrate" >rate setting in%</param> public void Setratechan        GE (float newrate) {soundtouch_setratechange (handle, newrate);         }//<summary>//sets new pitch control value.        </summary><param name= "Newpitch" >pitch setting.        Original pitch = 1.0, smaller values//represent lower pitches, larger values higher pitch.</param>        public void SetPitch (float newpitch) {soundtouch_setpitch (handle, Newpitch);  }///<summary>//sets pitch change in octaves compared to the original pitch//(-1.00).        +1.00 for +-one octave); </summary>//<param name= "Newpitch" >pitch setting in octaves</param> public void Set        Pitchoctaves (float newpitch) {soundtouch_setpitchoctaves (handle, Newpitch);  }///<summary>//sets pitch change in semi-tones compared to the original pitch//(-12).        +12 for +-one octave); </summary>//<param name= "Newpitch" >pitch setting in semitones</param> public void S Etpitchsemitones (float newpitch) {Soundtouch_setpitchSemitones (handle, Newpitch);        }//<summary>//Int16 version of Soundtouch_putsamples (): This accept int16 (short) sample data And internally converts it to float format before processing//</summary>//<param Nam E= "Samples" >sample input buffer.</param>//<param name= "NumSamples" >number of Sample frames in BUF Fer. Notice//, multi-channel sound a single///sample frame contains data for all CHANNELS.&LT ;/param> public void putsamples_i16 (short[] samples, uint numsamples) {Soundtouch_putsamples_        I16 (handle, samples, numsamples); }//<summary>//changes a setting controlling the processing system behaviour.        See the///' Setting_ ' defines for available SETTING ID ' s. </summary>//<param name= "Settingid" >setting ID number. See Setting_ ... defines.</param>///<param name= "value" New setting value></param>//<returns>nonzero If successful, otherwise zer o</returns> public int setsetting (int settingid, int value) {return soundtouch_setsetting (        Handle, Settingid, value); }//<summary>//Reads A setting controlling the processing system behaviour.        See the///' Setting_ ' defines for available SETTING ID ' s. </summary>//<param name= "Settingid" >setting ID number</param>//<returns>th e setting value</returns> public int soundtouch_getsetting (int settingid) {return Soundtou        Ch_getsetting (handle, Settingid);        }//<summary>//Returns number of samples currently unprocessed in SoundTouch internal buffer </summary>//<returns>number of Sample frames</returns> public uint Numunprocesse      Dsamples ()  {return soundtouch_numunprocessedsamples (handle);         }///<summary>//Int16 version of Soundtouch_receivesamples (): This converts internal float samples Into Int16 (short) return data type///</summary>//<param name= "Outbuffer" >buffe R where to copy output samples.</param>//<param name= "Maxsamples" >how many samples to receive at max .</param>//<returns>number of received sample frames</returns> public uint Soundtouch_r ECEIVESAMPLES_I16 (short[] outbuffer, uint maxsamples) {return soundtouch_receivesamples_i16 (handle, out        Buffer, Maxsamples);        }//<summary>//Check If there aren ' t any samples available for outputting. </summary>//<returns>nonzero If there aren ' t any samples available for Outputting</returns&gt        ; public int IsEmpty () {return soundTouch_isempty (handle); } [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl, EntryPoint = "Soundtouch_getversionid")  ]///<summary>//Get SoundTouch Library version Id///</summary> 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_de        Stroyinstance (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_se        Trate (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_se        Tratechange (IntPtr H, float newrate); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern void Soundtouch_se        Ttempochange (IntPtr H, float newtempo); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern void Soundtouch_se        Tpitch (IntPtr H, float newpitch); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern void Soundtouch_se        Tpitchoctaves (IntPtr H, float newpitch); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern void Soundtouch_se        Tpitchsemitones (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_se        Tsamplerate (IntPtr H, uint srate); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern void Soundtouch_fl        Ush (IntPtr h); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern void Soundtouch_pu        Tsamples (IntPtr H, float[] samples, uint numsamples); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern void Soundtouch_pu        TSAMPLES_I16 (IntPtr H, short[] samples, uint numsamples); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern void Soundtouch_cl        Ear (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_get        Setting (IntPtr h, int settingid); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern uint Soundtouch_nu        Munprocessedsamples (IntPtr h); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern uint Soundtouch_re        Ceivesamples (IntPtr H, float[] outbuffer, uint maxsamples); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern uint Soundtouch_re        CEIVESAMPLES_I16 (IntPtr H, short[] outbuffer, uint maxsamples); [DllImport ("SoundTouch.dll", CallingConvention = callingconvention.cdecl)] private static extern uint Soundtouch_nu        MSamples (IntPtr h); [DllImport ("SoundTouch.dll", CallingConvention = CallingcoNvention.    CDECL)] private static extern int soundtouch_isempty (IntPtr h); }

SOURCE Download (Https://files.cnblogs.com/files/leoxjy/demo.zip)

C # recording and pitch modulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.