Exemplo n.º 1
0
        private int CalculateVolume(SoundChannel soundChannel)
        {
            short[] dissolveData = null;

            switch (this.Preferences.SoundDissolve)
            {
            case 2:
                dissolveData = this.dissolveDataV2;
                break;

            case 3:
            default:
                dissolveData = this.dissolveDataV3;
                break;
            }

            int volume = soundChannel.Attenuation;

            if (volume != 0x0f)
            {
                if (soundChannel.DissolveCount != 0xffff)
                {
                    int val = dissolveData[soundChannel.DissolveCount];
                    if (val == -100)
                    {
                        // end of list
                        soundChannel.DissolveCount = 0xffff;
                        soundChannel.Attenuation   = soundChannel.AttenuationCopy;
                        volume = soundChannel.Attenuation;
                    }
                    else
                    {
                        soundChannel.DissolveCount++;

                        volume += val;
                        if (volume < 0)
                        {
                            volume = 0;
                        }

                        if (volume > 0x0f)
                        {
                            volume = 0x0f;
                        }

                        soundChannel.AttenuationCopy = volume;

                        volume &= 0x0f;
                        if (this.Preferences.SoundReadVariable)
                        {
                            volume += this.State.Variables[Variables.SoundVolume];
                        }

                        if (volume > 0x0f)
                        {
                            volume = 0x0f;
                        }
                    }
                }

                if (volume < 8)
                {
                    volume += 2;
                }
            }

            return(volume);
        }
Exemplo n.º 2
0
        internal int FillChannel(int channel, Tone tone)
        {
            if (!this.State.Flags[Flags.SoundOn])
            {
                return(-1);
            }

            SoundChannel soundChannel = this.soundChannels[channel];

            if (soundChannel.Avail == 0)
            {
                return(-1);
            }

            byte[] soundData = soundChannel.Data;

            while (soundChannel.Duration == 0 && soundChannel.Duration != 0xffff)
            {
                int offset = soundChannel.DataIndex;

                if (offset >= (soundData.Length - 2))
                {
                    soundChannel.Duration = 0xffff;
                    break;
                }

                soundChannel.Duration = (soundData[offset + 1] * 0x100) + soundData[offset];
                if (soundChannel.Duration != 0 && soundChannel.Duration != 0xffff)
                {
                    // Only tone channels dissolve
                    if (channel != 3 && this.Preferences.SoundDissolve != 0)
                    {
                        soundChannel.DissolveCount = 0;
                    }

                    // Volume
                    soundChannel.Attenuation = soundData[offset + 4] & 0x0f;

                    // Frequency
                    if (channel < 3)
                    {
                        soundChannel.FreqCount   = (ushort)(soundData[offset + 2] & 0x3f);
                        soundChannel.FreqCount <<= 4;
                        soundChannel.FreqCount  |= (byte)(soundData[offset + 3] & 0x0f);
                        soundChannel.GenType     = PcmSoundDriver.GenerateTone;
                    }
                    else
                    {
                        byte noiseType = (byte)(soundData[offset + 3] & 0x04);
                        soundChannel.GenType = noiseType != 0 ? PcmSoundDriver.GenerateWhite : PcmSoundDriver.GeneratePeriod;

                        int noiseFreq = (byte)(soundData[offset + 3] & 0x03);
                        switch (noiseFreq)
                        {
                        case 0:
                            soundChannel.FreqCount = 32;
                            break;

                        case 1:
                            soundChannel.FreqCount = 64;
                            break;

                        case 2:
                            soundChannel.FreqCount = 128;
                            break;

                        case 3:
                            soundChannel.FreqCount = this.soundChannels[2].FreqCount * 2;
                            break;
                        }
                    }
                }

                soundChannel.DataIndex += 5;
            }

            if (soundChannel.Duration != 0xffff)
            {
                tone.FrequencyCount = soundChannel.FreqCount;
                tone.Attenuation    = this.CalculateVolume(soundChannel);
                tone.Type           = soundChannel.GenType;
                soundChannel.Duration--;
            }
            else
            {
                soundChannel.Avail           = 0;
                soundChannel.Attenuation     = 0x0f; // silent
                soundChannel.AttenuationCopy = 0x0f;
                return(-1);
            }

            return(0);
        }