コード例 #1
0
ファイル: Utilities.cs プロジェクト: michaelbehner96/Xyzzxyz
        public static void ProcessAudioWave(string fileIn, string fileOut, float newTempo = 1, float newPitch = 1, float newRate = 1)
        {
            WaveFileReader reader = new WaveFileReader(fileIn);

            int numChannels = reader.WaveFormat.Channels;

            if (numChannels > 2)
                throw new Exception("SoundTouch supports only mono or stereo.");

            int sampleRate = reader.WaveFormat.SampleRate;

            int bitPerSample = reader.WaveFormat.BitsPerSample;

            const int BUFFER_SIZE = 1024 * 16;

            SoundStretcher stretcher = new SoundStretcher(sampleRate, numChannels);
            WaveFileWriter writer = new WaveFileWriter(fileOut, new WaveFormat(sampleRate, 16, numChannels));

            stretcher.Tempo = newTempo;
            stretcher.Pitch = newPitch;
            stretcher.Rate = newRate;

            byte[] buffer = new byte[BUFFER_SIZE];
            short[] buffer2 = null;

            if (bitPerSample != 16 && bitPerSample != 8)
            {
                throw new Exception("Not implemented yet.");
            }

            if (bitPerSample == 8)
            {
                buffer2 = new short[BUFFER_SIZE];
            }

            bool finished = false;

            while (true)
            {
                int bytesRead = 0;
                if (!finished)
                {
                    bytesRead = reader.Read(buffer, 0, BUFFER_SIZE);

                    if (bytesRead == 0)
                    {
                        finished = true;
                        stretcher.Flush();
                    }
                    else
                    {
                        if (bitPerSample == 16)
                        {
                            stretcher.PutSamplesFromBuffer(buffer, 0, bytesRead);
                        }
                        else if (bitPerSample == 8)
                        {
                            for (int i = 0; i < BUFFER_SIZE; i++)
                                buffer2[i] = (short)((buffer[i] - 128) * 256);
                            stretcher.PutSamples(buffer2);
                        }
                    }
                }
                bytesRead = stretcher.ReceiveSamplesToBuffer(buffer, 0, BUFFER_SIZE);
                writer.Write(buffer, 0, bytesRead);

                if (finished && bytesRead == 0)
                    break;
            }

            reader.Close();
            writer.Close();
        }
コード例 #2
0
        void changePitchNTempo(String fileName, String fileOut, float newTempo, float newPitch)
        {
            WaveFileReader reader      = new WaveFileReader(fileName);
            int            numChannels = reader.WaveFormat.Channels;

            if (numChannels > 2)
            {
                throw new Exception("SoundTouch supports only mono or stereo.");
            }

            int       sampleRate   = reader.WaveFormat.SampleRate;
            int       bitPerSample = reader.WaveFormat.BitsPerSample;
            const int BUFFER_SIZE  = 1024 * 16;

            SoundStretcher stretcher = new SoundStretcher(sampleRate, numChannels);
            WaveFileWriter writer    = new WaveFileWriter(fileOut, new WaveFormat(sampleRate, 16, numChannels));

            stretcher.Tempo = newTempo;
            stretcher.Pitch = newPitch;

            byte[]  buffer  = new byte[BUFFER_SIZE];
            short[] buffer2 = null;

            if (bitPerSample != 16 && bitPerSample != 8)
            {
                throw new Exception("Not implemented yet.");
            }

            if (bitPerSample == 8)
            {
                buffer2 = new short[BUFFER_SIZE];
            }

            bool finished = false;

            while (true)
            {
                int bytesRead = 0;
                if (!finished)
                {
                    bytesRead = reader.Read(buffer, 0, BUFFER_SIZE);

                    if (bytesRead == 0)
                    {
                        finished = true;
                        stretcher.Flush();
                    }
                    else
                    {
                        if (bitPerSample == 16)
                        {
                            stretcher.PutSamplesFromBuffer(buffer, 0, bytesRead);
                        }
                        else if (bitPerSample == 8)
                        {
                            for (int i = 0; i < BUFFER_SIZE; i++)
                            {
                                buffer2[i] = (short)((buffer[i] - 128) * 256);
                            }

                            stretcher.PutSamples(buffer2);
                        }
                    }
                }

                bytesRead = stretcher.ReceiveSamplesToBuffer(buffer, 0, BUFFER_SIZE);
                writer.Write(buffer, 0, bytesRead);

                if (finished && bytesRead == 0)
                {
                    break;
                }
            }

            reader.Close();
            writer.Close();
        }
コード例 #3
0
ファイル: PhraseFile.cs プロジェクト: postworthy/SpanishAudio
        private static byte[] SlowAudio(byte[] mp3, float newTempo = 0.95f, float newPitch = 1.2f, float newRate = .8f)
        {
            const int BUFFER_SIZE = 1024 * 16;
            byte[] buffer = new byte[BUFFER_SIZE];
            var format = new WaveFormat();

            try
            {
                using (var output = new MemoryStream())
                {

                    File.Delete("temp.wav");
                    File.Delete("temp2.wav");

                    using (var mp3Stream = new MemoryStream(mp3))
                    using (Mp3FileReader reader = new Mp3FileReader(mp3Stream))
                    {
                        WaveFileWriter.CreateWaveFile("temp.wav", reader);
                    }

                    using (WaveFileReader reader = new WaveFileReader("temp.wav"))
                    {
                        int numChannels = reader.WaveFormat.Channels;

                        if (numChannels > 2)
                            throw new Exception("SoundTouch supports only mono or stereo.");

                        int sampleRate = reader.WaveFormat.SampleRate;

                        int bitPerSample = reader.WaveFormat.BitsPerSample;

                        SoundStretcher stretcher = new SoundStretcher(sampleRate, numChannels);
                        var writer = new NAudio.Lame.LameMP3FileWriter(output, new WaveFormat(sampleRate, 16, numChannels), 128);

                        stretcher.Tempo = newTempo;
                        stretcher.Pitch = newPitch;
                        stretcher.Rate = newRate;

                        short[] buffer2 = null;

                        if (bitPerSample != 16 && bitPerSample != 8)
                        {
                            throw new Exception("Not implemented yet.");
                        }

                        if (bitPerSample == 8)
                        {
                            buffer2 = new short[BUFFER_SIZE];
                        }

                        bool finished = false;

                        while (true)
                        {
                            int bytesRead = 0;
                            if (!finished)
                            {
                                bytesRead = reader.Read(buffer, 0, BUFFER_SIZE);

                                if (bytesRead == 0)
                                {
                                    finished = true;
                                    stretcher.Flush();
                                }
                                else
                                {
                                    if (bitPerSample == 16)
                                    {
                                        stretcher.PutSamplesFromBuffer(buffer, 0, bytesRead);
                                    }
                                    else if (bitPerSample == 8)
                                    {
                                        for (int i = 0; i < BUFFER_SIZE; i++)
                                            buffer2[i] = (short)((buffer[i] - 128) * 256);
                                        stretcher.PutSamples(buffer2);
                                    }
                                }
                            }
                            bytesRead = stretcher.ReceiveSamplesToBuffer(buffer, 0, BUFFER_SIZE);
                            writer.Write(buffer, 0, bytesRead);

                            if (finished && bytesRead == 0)
                                break;
                        }
                        reader.Close();
                        writer.Close();
                    }

                    return output.ToArray();
                }
            }
            finally
            {
                File.Delete("temp.wav");
                File.Delete("temp2.wav");
            }

            /////////////////////////////
        }
コード例 #4
0
ファイル: Model.cs プロジェクト: MegalithAgnusDei/SMLooper
        public byte[] GetRatedData(byte[] data, double rate, WaveFormat wf)
        {
            MemoryStream   memRead     = new MemoryStream(data);
            WaveFileReader reader      = new WaveFileReader(memRead);
            int            numChannels = wf.Channels;

            if (numChannels > 2)
            {
                throw new Exception("SoundTouch supports only mono or stereo.");
            }

            int sampleRate = wf.SampleRate;

            int bitPerSample = wf.BitsPerSample;

            const int BUFFER_SIZE = 1024 * 16;

            SoundStretcher stretcher = new SoundStretcher(sampleRate, numChannels);
            MemoryStream   memWrite  = new MemoryStream();
            WaveFileWriter writer    = new WaveFileWriter(memWrite, new WaveFormat(sampleRate, 16, numChannels));

            stretcher.Tempo = (float)rate;

            byte[]  buffer  = new byte[BUFFER_SIZE];
            short[] buffer2 = null;

            if (bitPerSample != 16 && bitPerSample != 8)
            {
                throw new Exception("Not implemented yet.");
            }

            if (bitPerSample == 8)
            {
                buffer2 = new short[BUFFER_SIZE];
            }

            bool finished = false;

            while (true)
            {
                int bytesRead = 0;
                if (!finished)
                {
                    bytesRead = reader.Read(buffer, 0, BUFFER_SIZE);

                    if (bytesRead == 0)
                    {
                        finished = true;
                        stretcher.Flush();
                    }
                    else
                    {
                        if (bitPerSample == 16)
                        {
                            stretcher.PutSamplesFromBuffer(buffer, 0, bytesRead);
                        }
                        else if (bitPerSample == 8)
                        {
                            for (int i = 0; i < BUFFER_SIZE; i++)
                            {
                                buffer2[i] = (short)((buffer[i] - 128) * 256);
                            }
                            stretcher.PutSamples(buffer2);
                        }
                    }
                }
                bytesRead = stretcher.ReceiveSamplesToBuffer(buffer, 0, BUFFER_SIZE);
                writer.Write(buffer, 0, bytesRead);

                if (finished && bytesRead == 0)
                {
                    break;
                }
            }

            reader.Close();
            writer.Close();
            return(memWrite.ToArray());
        }