This project has moved and is read-only. For the latest updates, please go here.

Calculate silance bit of a mp3 file.

Apr 17, 2014 at 2:02 PM
Edited Apr 17, 2014 at 2:14 PM
Hi mark,
I want to calculate cuein(I also trying to get the start position to remove silence at the beginning of the mp3 file) and cueout(I also trying to get the end position to remove silence at the end of the mp3 file).I have done this.But problem is that to execute below method CPU usage goes to 50% fo ra single player(i have implemented multiplier concept).If any other way calculate this silence bit please share with me(very urgent).If a song is 3 mb then wave.Length goes to 41515315.
private void CalculateCueInOutTime(string EncryptFileName, Playlist m_objPlaylist)
        {
            try
            {
                //double MaxDBValue = 0.00;
                var plugin = GetPluginForFile(EncryptFileName);
                if (plugin == null)
                {
                    throw new InvalidOperationException("Unsupported file extension");
                }

                //  Mp3FileReader reader = new Mp3FileReader(EncryptFileName);
                //  WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(reader);

                WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(plugin.CreateWaveStream(EncryptFileName));
                
                NAudio.Wave.WaveChannel32 wave = new NAudio.Wave.WaveChannel32(pcm);
                byte[] buffer = new byte[16384];
                int read = 0;
                int count = 0;
                int totalCount = 0;
                List<double> lstStore = new List<double>();

                while (wave.Position < wave.Length)
                {
                    read = wave.Read(buffer, 0, 16384);
                    for (int i = 0; i < read / 8; i++)
                    {
                        if ((BitConverter.ToSingle(buffer, i * 8) < -0.06) || (BitConverter.ToSingle(buffer, i * 8) > 0.06))
                        {
                            if (count == 0)
                            {
                                count = totalCount;
                            }
                        }
                        //if (BitConverter.ToSingle(buffer, i * 8) > MaxDBValue)
                        //{
                        //    MaxDBValue = Convert.ToDouble(BitConverter.ToSingle(buffer, i * 8));
                        //}
                        lstStore.Add(BitConverter.ToSingle(buffer, i * 8));
                        totalCount++;
                    }

                    //for (int i = 0; i < read / 4; i++)
                    //{
                    //    MaxPeakValue = Math.Max(MaxPeakValue, BitConverter.ToSingle(buffer, i * 4));
                    //}
                }
                //MaxPeakofSong.Add(MaxPeakValue);
                Double CueInTime = (wave.TotalTime.TotalSeconds * count) / totalCount;
                int CueMin = (int)CueInTime / 60;
                double CueSec = (CueInTime - CueMin * 60);
                string s_CueInTime = String.Format("{0:00}:{1:00.00}", CueMin, CueSec);

                count = 0;
                for (int i = lstStore.Count - 1; i >= 0; i--)
                {
                    if ((lstStore[i] < -0.09) || (lstStore[i] > 0.09))   //0.07 //0.14
                    {
                        if (count == 0)
                        {
                            count = lstStore.Count - i - 1;
                            break;
                        }
                    }
                }
                Double CueOutTime = (wave.TotalTime.TotalSeconds * count) / totalCount;
                CueMin = (int)CueOutTime / 60;
                CueSec = (CueOutTime - CueMin * 60);
                string s_CueOutTime = String.Format("{0:00}:{1:00.00}", CueMin, CueSec);
}
May 6, 2014 at 9:08 PM
I'd recommend you start by using AudioFileReader and read using the float[] overload for an easier way to access each sample.