Beispiel #1
0
        public static void Mp3Split(string mp3_source, int[] miliseconds, string[] mp3_destinations, bool removeSilence = false)
        {
            string        name   = Path.GetFileNameWithoutExtension(mp3_source);
            Mp3FileReader reader = new Mp3FileReader(mp3_source);

            //double duration = (reader.TotalTime).TotalMilliseconds;
            //int parts = (int)Math.Ceiling((double)duration / miliseconds);

            if (reader == null || miliseconds.Length + 1 != mp3_destinations.Length)
            {
                Output.WriteLine("Mp3 Output destinations missing !");
                return;
            }



            double   currentTime;
            string   destination;
            Mp3Frame frame;

            for (int i = 0; i < mp3_destinations.Length; i++)
            {
                currentTime = 0;
                destination = mp3_destinations[i];
                frame       = reader.ReadNextFrame();


                Stream memory = new MemoryStream();

                while (frame != null && (i >= miliseconds.Length || currentTime < miliseconds[i]))
                {
                    currentTime = (reader.CurrentTime).TotalMilliseconds;

                    memory.Write(frame.RawData, 0, frame.RawData.Length);
                    frame = reader.ReadNextFrame();
                }



                byte[] result;

                if (removeSilence)
                {
                    result = Streams.ToArray(Converter.Mp3RemoveSilence(memory));
                }
                else
                {
                    result = Streams.ToArray(memory);
                }

                if (File.Exists(destination))
                {
                    File.Delete(destination);
                }

                if (Path.GetExtension(destination).ToLower().Contains("wav"))
                {
                    Stream     temp = Streams.ToMemory(result);
                    WaveStream pcm  = Converter.Mp3ToWAV(temp);
                    if (pcm == null)
                    {
                        continue;
                    }


                    //pcm = WavLevel16bit(pcm, 50, short.MaxValue);
                    //WaveStream pcm2 = pcm;//
                    pcm = Converter.WavExtrapolate16bit(pcm, SampleRates.f44kHz);


                    //pcm = WavRemoveSilence2(pcm, 15, 50);

                    byte[] ba = Converter.WaveStreamToArray(pcm);
                    //double[] da = Converter.WaveStreamToDoubleArray(pcm, true);
                    double[] da = Converter.WavToDoubleArray(ba, true);

                    /// Fundamentals -  85   Hz -  250 Hz
                    /// Vowels -        350  Hz -  2k  Hz
                    /// Consonants -    1.5k Hz -  4k  Hz

                    da = AMath.FilterBandPassRLC(da, pcm.WaveFormat.SampleRate, 85, 4000, 1);
                    //da = AMath.FilterBandPassRLC(da, pcm.WaveFormat.SampleRate, 85, 250, 1);

                    byte[] ba2 = Converter.WavToByteArray(da, true);

                    pcm = Converter.ArrayToWaveStream(ba2, pcm.WaveFormat);

                    pcm = WavLevel16bit(pcm, 100, short.MaxValue);

                    /*double ave = Converter.WavGetAbsAverage16bit(pcm, false);
                     * double max = Converter.WavGetAbsMax16bit(pcm);
                     * long countNonZero = WavGetSamplesCount16bit(pcm, 1, -1);
                     * long countAbsAverage = WavGetSamplesCount16bit(pcm, (int)ave, -(int)ave);
                     *
                     */


                    if (pcm != null)
                    {
                        WaveFileWriter.CreateWaveFile(destination, pcm);
                        temp.Close();
                        pcm.Close();
                    }
                }
                else
                {
                    FileStream stream = new FileStream(destination, FileMode.Create, FileAccess.Write);
                    stream.Write(result, 0, result.Length);
                    stream.Close();
                }
                memory.Close();
            }

            reader.Close();
        }