Пример #1
0
        public void DoReplayGainAnalysis()
        {
            try
            {
                Controller.ShowMessage("Starting volume leveling analysis...");

                List <List <Track> > albums = partitionByAlbum(tracks);

                byte[]  buffer = new byte[READ_BUFFER_SIZE];
                float[] left   = new float[READ_BUFFER_SIZE / 4];
                float[] right  = new float[READ_BUFFER_SIZE / 4];
                int     numSamples;

                foreach (List <Track> album in albums)
                {
                    try
                    {
                        if (rga.InitGainAnalysis(album[0].SampleRate))
                        {
                            foreach (Track t in album)
                            {
                                AudioStream stream = new AudioStreamFile(t, 0, new float[10], 10, false, ReplayGainMode.Off);

                                while ((numSamples = stream.Read(buffer, 0, READ_BUFFER_SIZE)) > 0)
                                {
                                    int inputBufferSize = numSamples / 4;
                                    for (int i = 0; i < inputBufferSize; i++)
                                    {
                                        left[i]  = AudioStreamBass.from16Bit(buffer[i * 4 + 1] * 0x100 + buffer[i * 4]);
                                        right[i] = AudioStreamBass.from16Bit(buffer[i * 4 + 3] * 0x100 + buffer[i * 4 + 2]);
                                    }
                                    rga.AnalyzeSamples(left, right, inputBufferSize);
                                    if (cancel)
                                    {
                                        callback();
                                        return;
                                    }
                                }
                                stream.Close();
                                t.ReplayGainTrack = rga.GetTrackGain();
                                numTracksDone++;
                                Controller.ShowMessage(String.Format("{0}/{1}: Volume analyzed for '{2}'", numTracksDone.ToString(), numTracks.ToString(), t.ToShortString()));
                            }
                            float albumGain = rga.GetAlbumGain();
                            foreach (Track t in album)
                            {
                                t.ReplayGainAlbum = albumGain;
                            }
                            if (Setting.WriteReplayGainTags)
                            {
                                foreach (Track t in album)
                                {
                                    t.ChangeType |= ChangeType.WriteTags;
                                }
                                TrackWriter.AddToUnsavedTracks(album);
                                TrackWriter.Start();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
            callback();
        }