예제 #1
0
        protected override void UpdateAfter()
        {
            foreach (var entry in _psgEntries)
            {
                entry.WasActive = entry.Active;
                entry.Active    = false;
            }

            bool sync = false;

            lvPsgWaveforms.BeginUpdate();
            lvChannels.BeginUpdate();

            for (int i = 0; i < 6; i++)
            {
                var ch = PCE.PSG.Channels[i];

                // these conditions mean a sample isn't playing
                if (!ch.Enabled)
                {
                    lvChannels.Items[i].SubItems[1].Text = "-";
                    lvChannels.Items[i].SubItems[2].Text = "-";
                    lvChannels.Items[i].SubItems[3].Text = "(disabled)";
                    _lastSamples[i] = null;
                    continue;
                }
                if (ch.DDA)
                {
                    lvChannels.Items[i].SubItems[1].Text = "-";
                    lvChannels.Items[i].SubItems[2].Text = "-";
                    lvChannels.Items[i].SubItems[3].Text = "(DDA)";
                    _lastSamples[i] = null;
                    continue;
                }
                lvChannels.Items[i].SubItems[1].Text = ch.Volume.ToString();
                lvChannels.Items[i].SubItems[2].Text = ch.Frequency.ToString();
                if (ch.NoiseChannel)
                {
                    lvChannels.Items[i].SubItems[3].Text = "(noise)";
                    _lastSamples[i] = null;
                    continue;
                }

                if (ch.Volume == 0)
                {
                    _lastSamples[i] = null;
                    continue;
                }

                lvChannels.Items[i].SubItems[3].Text = "-";

                // ok, a sample is playing. copy out the waveform
                short[] waveform = (short[])ch.Wave.Clone();

                // hash it
                var ms = new MemoryStream(_waveformTemp);
                var bw = new BinaryWriter(ms);
                foreach (var s in waveform)
                {
                    bw.Write(s);
                }

                bw.Flush();
                var md5 = MD5Checksum.ComputeDigestHex(_waveformTemp);

                if (!_psgEntryTable.TryGetValue(md5, out var entry))
                {
                    entry = new PsgEntry
                    {
                        Name     = md5,
                        WaveForm = waveform,
                        Active   = true,
                        HitCount = 1,
                        Index    = _psgEntries.Count
                    };
                    _psgEntries.Add(entry);
                    _psgEntryTable[md5] = entry;
                    sync            = true;
                    _lastSamples[i] = entry;
                }
                else
                {
                    entry.Active = true;

                    // are we playing the same sample as before?
                    if (_lastSamples[i] != entry)
                    {
                        _lastSamples[i] = entry;
                        entry.HitCount++;
                        if (entry.Index < lvPsgWaveforms.Items.Count)
                        {
                            lvPsgWaveforms.Items[entry.Index].SubItems[1].Text = entry.HitCount.ToString();
                        }
                        else
                        {
                            sync = true;
                        }
                    }
                }

                lvChannels.Items[i].SubItems[3].Text = entry.Name;
            }

            if (sync)
            {
                SyncLists();
            }
            lvPsgWaveforms.EndUpdate();
            lvChannels.EndUpdate();
        }