/// <summary>
 /// Helper function to go from IWaveProvider to a SampleProvider
 /// Must already be PCM or IEEE float
 /// </summary>
 /// <param name="waveProvider">The WaveProvider to convert</param>
 /// <returns>A sample provider</returns>
 public static ISampleProvider ConvertWaveProviderIntoSampleProvider(IWaveProvider waveProvider)
 {
     ISampleProvider sampleProvider;
     if (waveProvider.WaveFormat.Encoding == WaveFormatEncoding.Pcm)
     {
         // go to float
         if (waveProvider.WaveFormat.BitsPerSample == 8)
         {
             sampleProvider = new Pcm8BitToSampleProvider(waveProvider);
         }
         else if (waveProvider.WaveFormat.BitsPerSample == 16)
         {
             sampleProvider = new Pcm16BitToSampleProvider(waveProvider);
         }
         else if (waveProvider.WaveFormat.BitsPerSample == 24)
         {
             sampleProvider = new Pcm24BitToSampleProvider(waveProvider);
         }
         else
         {
             throw new InvalidOperationException("Unsupported operation");
         }
     }
     else if (waveProvider.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat)
     {
         sampleProvider = new WaveToSampleProvider(waveProvider);
     }
     else
     {
         throw new ArgumentException("Unsupported source encoding");
     }
     return sampleProvider;
 }
示例#2
1
 public static SampleSource CreateFromWaveFile(string fileName)
 {
     using (var reader = new WaveFileReader(fileName))
     {
         ISampleProvider sp;
         int sourceSamples;
         if (reader.WaveFormat.Encoding == WaveFormatEncoding.Pcm)
         {
             if (reader.WaveFormat.BitsPerSample == 16)
             {
                 sp = new Pcm16BitToSampleProvider(reader);
                 sourceSamples = (int)(reader.Length / 2);
             }
             else if (reader.WaveFormat.BitsPerSample == 24)
             {
                 sp = new Pcm24BitToSampleProvider(reader);
                 sourceSamples = (int)(reader.Length / 3);
             }
             else
             {
                 throw new ArgumentException("Currently only 16 or 24 bit PCM samples are supported");
             }
         }
         else if (reader.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat)
         {
             sp = new WaveToSampleProvider(reader);
             sourceSamples = (int)(reader.Length / 4);
         }
         else
         {
             throw new ArgumentException("Must be PCM or IEEE float");
         }
         float[] sampleData = new float[sourceSamples];
         int n = sp.Read(sampleData, 0, sourceSamples);
         if (n != sourceSamples)
         {
             throw new InvalidOperationException(String.Format("Couldn't read the whole sample, expected {0} samples, got {1}", n, sourceSamples));
         }
         SampleSource ss = new SampleSource(sampleData, sp.WaveFormat);
         return ss;
     }
 }
示例#3
0
        /// <summary>
        /// Helper function to go from IWaveProvider to a SampleProvider
        /// Must already be PCM or IEEE float
        /// </summary>
        /// <param name="waveProvider">The WaveProvider to convert</param>
        /// <returns>A sample provider</returns>
        public static ISampleProvider ConvertWaveProviderIntoSampleProvider(IWaveProvider waveProvider)
        {
            ISampleProvider sampleProvider;

            if (waveProvider.WaveFormat.Encoding == WaveFormatEncoding.Pcm)
            {
                // go to float
                if (waveProvider.WaveFormat.BitsPerSample == 8)
                {
                    sampleProvider = new Pcm8BitToSampleProvider(waveProvider);
                }
                else if (waveProvider.WaveFormat.BitsPerSample == 16)
                {
                    sampleProvider = new Pcm16BitToSampleProvider(waveProvider);
                }
                else if (waveProvider.WaveFormat.BitsPerSample == 24)
                {
                    sampleProvider = new Pcm24BitToSampleProvider(waveProvider);
                }
                else
                {
                    throw new InvalidOperationException("Unsupported operation");
                }
            }
            else if (waveProvider.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat)
            {
                sampleProvider = new WaveToSampleProvider(waveProvider);
            }
            else
            {
                throw new ArgumentException("Unsupported source encoding");
            }
            return(sampleProvider);
        }
示例#4
0
        public static ISampleProvider ConvertWaveProviderIntoSampleProvider(IWaveProvider waveProvider)
        {
            ISampleProvider result;

            if (waveProvider.WaveFormat.Encoding == WaveFormatEncoding.Pcm)
            {
                if (waveProvider.WaveFormat.BitsPerSample == 8)
                {
                    result = new Pcm8BitToSampleProvider(waveProvider);
                }
                else if (waveProvider.WaveFormat.BitsPerSample == 16)
                {
                    result = new Pcm16BitToSampleProvider(waveProvider);
                }
                else if (waveProvider.WaveFormat.BitsPerSample == 24)
                {
                    result = new Pcm24BitToSampleProvider(waveProvider);
                }
                else
                {
                    if (waveProvider.WaveFormat.BitsPerSample != 32)
                    {
                        throw new InvalidOperationException("Unsupported bit depth");
                    }
                    result = new Pcm32BitToSampleProvider(waveProvider);
                }
            }
            else
            {
                if (waveProvider.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat)
                {
                    throw new ArgumentException("Unsupported source encoding");
                }
                if (waveProvider.WaveFormat.BitsPerSample == 64)
                {
                    result = new WaveToSampleProvider64(waveProvider);
                }
                else
                {
                    result = new WaveToSampleProvider(waveProvider);
                }
            }
            return(result);
        }
        /// <summary>
        /// Initialises a new instance of SampleChannel
        /// </summary>
        /// <param name="waveProvider">Source wave provider, must be PCM or IEEE</param>
        public SampleChannel(IWaveProvider waveProvider)
        {
            ISampleProvider sampleProvider;

            if (waveProvider.WaveFormat.Encoding == WaveFormatEncoding.Pcm)
            {
                // go to float
                if (waveProvider.WaveFormat.BitsPerSample == 8)
                {
                    sampleProvider = new Pcm8BitToSampleProvider(waveProvider);
                }
                else if (waveProvider.WaveFormat.BitsPerSample == 16)
                {
                    sampleProvider = new Pcm16BitToSampleProvider(waveProvider);
                }
                else if (waveProvider.WaveFormat.BitsPerSample == 24)
                {
                    sampleProvider = new Pcm24BitToSampleProvider(waveProvider);
                }
                else
                {
                    throw new InvalidOperationException("Unsupported operation");
                }
            }
            else if (waveProvider.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat)
            {
                sampleProvider = new WaveToSampleProvider(waveProvider);
            }
            else
            {
                throw new ArgumentException("Unsupported source encoding");
            }
            if (sampleProvider.WaveFormat.Channels == 1)
            {
                sampleProvider = new MonoToStereoSampleProvider(sampleProvider);
            }
            this.waveFormat = sampleProvider.WaveFormat;
            // let's put the meter before the volume (useful for drawing waveforms)
            this.preVolumeMeter = new MeteringSampleProvider(sampleProvider);
            this.volumeProvider = new VolumeSampleProvider(preVolumeMeter);
        }
 /// <summary>
 /// Initialises a new instance of SampleChannel
 /// </summary>
 /// <param name="waveProvider">Source wave provider, must be PCM or IEEE</param>
 public SampleChannel(IWaveProvider waveProvider)
 {
     ISampleProvider sampleProvider;
     if (waveProvider.WaveFormat.Encoding == WaveFormatEncoding.Pcm)
     {
         // go to float
         if (waveProvider.WaveFormat.BitsPerSample == 8)
         {
             sampleProvider = new Pcm8BitToSampleProvider(waveProvider);
         }
         else if (waveProvider.WaveFormat.BitsPerSample == 16)
         {
             sampleProvider = new Pcm16BitToSampleProvider(waveProvider);
         }
         else if (waveProvider.WaveFormat.BitsPerSample == 24)
         {
             sampleProvider = new Pcm24BitToSampleProvider(waveProvider);
         }
         else
         {
             throw new InvalidOperationException("Unsupported operation");
         }
     }
     else if (waveProvider.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat)
     {
         sampleProvider = new WaveToSampleProvider(waveProvider);
     }
     else
     {
         throw new ArgumentException("Unsupported source encoding");
     }
     if (sampleProvider.WaveFormat.Channels == 1)
     {
         sampleProvider = new MonoToStereoSampleProvider(sampleProvider);
     }
     this.waveFormat = sampleProvider.WaveFormat;
     // let's put the meter before the volume (useful for drawing waveforms)
     this.preVolumeMeter = new MeteringSampleProvider(sampleProvider);
     this.volumeProvider = new VolumeSampleProvider(preVolumeMeter);
 }
        public static byte[] ConvertAiffToMp3(byte[] aiffFile, string directory)
        {
            byte[] mp3bytes = null;
            var newFormat = new WaveFormat(bitRate, bitDepth, channels);
            try
            {
                uniqueTempFileCounter += 1;
                var tempFile = Path.Combine(directory, "tempdata"+ uniqueTempFileCounter +".wav");

                using (var ms = new MemoryStream(aiffFile))
                using (var rdr = new AiffFileReader(ms))
                {
                    if (rdr.WaveFormat.BitsPerSample == 24) //can't go from 24 bits aif to mp3 directly, create temporary 16 bit wav
                    {
                            ISampleProvider sampleprovider = new Pcm24BitToSampleProvider(rdr); //24 bit to sample
                            var resampler = new WdlResamplingSampleProvider(sampleprovider, sampleRate); //sample to new sample rate
                            WaveFileWriter.CreateWaveFile16(tempFile, resampler); //sample to actual wave file
                            mp3bytes = ConvertWavToMp3(tempFile, true); //file to mp3 bytes
                    }
                    else
                    {
                        using (var retMs = new MemoryStream())
                        using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, bitRate))
                        {
                            rdr.CopyTo(wtr);
                            mp3bytes = retMs.ToArray();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
            return mp3bytes;
        }
示例#8
0
        public static SampleSource[] CreateFromWaveFile(string fileName)
        {
            using (var reader = new WaveFileReader(fileName))
            {
                ISampleProvider sp;
                int sourceSamples;
                if (reader.WaveFormat.Encoding == WaveFormatEncoding.Pcm)
                {
                    if (reader.WaveFormat.BitsPerSample == 16)
                    {
                        sp = new Pcm16BitToSampleProvider(reader);
                        sourceSamples = (int)(reader.Length / 2);
                    }
                    else if (reader.WaveFormat.BitsPerSample == 24)
                    {
                        sp = new Pcm24BitToSampleProvider(reader);
                        sourceSamples = (int)(reader.Length / 3);
                    }
                    else
                    {
                        throw new ArgumentException("Currently only 16 or 24 bit PCM samples are supported");
                    }
                }
                else if (reader.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat)
                {
                    sp = new WaveToSampleProvider(reader);
                    sourceSamples = (int)(reader.Length / 4);
                }
                else
                {
                    throw new ArgumentException("Must be PCM or IEEE float");
                }
                float[] sampleData = new float[sourceSamples];
                int n = sp.Read(sampleData, 0, sourceSamples);
                if (n != sourceSamples)
                {
                    throw new InvalidOperationException(String.Format("Couldn't read the whole sample, expected {0} samples, got {1}", n, sourceSamples));
                }

                SampleSource[] result = new SampleSource[9];
                // Normal pitch
                int normal = 4;
                result[normal] = new SampleSource(sampleData, sp.WaveFormat);
                int pitch = 5;

                for (int currentPitch = 0; currentPitch < normal; currentPitch++, pitch--)
                {
                    float[] changedPitchData = new float[sampleData.Length/pitch];
                    for (int j = 0, i = 0; i  < sampleData.Length && j < changedPitchData.Length; i++)
                    {
                        if (i % pitch == 0)
                        {
                            changedPitchData[j] = sampleData[i];
                            j++;
                        }
                    }

                    result[currentPitch] = new SampleSource(changedPitchData, sp.WaveFormat);
                }

                pitch = 2;

                for (int currentPitch = normal+1; currentPitch < result.Length; currentPitch++, pitch++)
                {
                    float[] changedPitchData = new float[sampleData.Length * pitch];
                    for (int j = 0, i = 0; i < sampleData.Length && j < changedPitchData.Length; j++)
                    {
                        changedPitchData[j] = sampleData[i];

                        if (j % pitch == 0)
                        {
                            i++;
                        }
                    }

                    result[currentPitch] = new SampleSource(changedPitchData, sp.WaveFormat);
                }
                return result;
            }
        }