示例#1
0
        public void Write(AudioBuffer buff)
        {
            int bs        = Settings.PCM.BlockAlign;
            int buff_offs = 0;

            while (buff_offs < buff.Length)
            {
                if (mixbuff == null)
                {
                    mixbuff = mixer.LockEmptyBuffer(iSource);
                    mixbuff.source[iSource].Prepare(-1);
                    mixbuff.source[iSource].Length = 0;
                }

                int chunk = Math.Min(buff.Length - buff_offs, mixbuff.source[iSource].Size - mixbuff.source[iSource].Length);
                Buffer.BlockCopy(buff.Float, buff_offs * bs, mixbuff.source[iSource].Float, mixbuff.source[iSource].Length * bs, chunk * bs);
                mixbuff.source[iSource].Length += chunk;
                buff_offs += chunk;

                if (mixbuff.source[iSource].Length == mixbuff.source[iSource].Size)
                {
                    mixer.UnlockEmptyBuffer(mixbuff, iSource, volume);
                    mixbuff = null;
                }
            }

            samplePos += buff.Length;
        }
示例#2
0
 public MixingWriter(MixingSource mixer, int iSource)
 {
     this.mixer = mixer;
     this.iSource = iSource;
     this.samplePos = 0;
     this.mixbuff = null;
     this.volume = 1.0f;
 }
示例#3
0
 public MixingWriter(MixingSource mixer, int iSource)
 {
     this.m_settings = new AudioEncoderSettings(mixer.PCM);
     this.mixer      = mixer;
     this.iSource    = iSource;
     this.samplePos  = 0;
     this.mixbuff    = null;
     this.volume     = 1.0f;
 }
        public int Read(AudioBuffer result, int maxLength)
        {
            if (maxLength > (BufferSize - mixoffs) || maxLength < 0)
            {
                maxLength = (BufferSize - mixoffs);
            }

            result.Prepare(maxLength);

            if (mixbuff == null)
            {
                mixbuff = LockFilledBuffer();
            }

            float sumVolume = 0.0f;

            for (int iSource = 0; iSource < mixbuff.source.Length; iSource++)
            {
                if (mixbuff.filled[iSource])
                {
                    sumVolume += mixbuff.volume[iSource];
                }
            }
            for (int iSource = 0; iSource < mixbuff.source.Length; iSource++)
            {
                volume[iSource] = mixbuff.filled[iSource] ? mixbuff.volume[iSource] / Math.Max(1.0f, sumVolume) : 0.0f;
            }
            for (int iSmp = 0; iSmp < result.Length; iSmp++)
            {
                for (int iChan = 0; iChan < result.PCM.ChannelCount; iChan++)
                {
                    float sample = 0.0f;
                    for (int iSource = 0; iSource < mixbuff.source.Length; iSource++)
                    {
                        sample += mixbuff.source[iSource].Float[mixoffs + iSmp, iChan] * volume[iSource];
                    }
                    result.Float[iSmp, iChan] = sample;
                }
            }
            mixoffs += result.Length;
            if (mixoffs == BufferSize)
            {
                UnlockFilledBuffer(mixbuff);
                mixbuff = null;
                mixoffs = 0;
            }
            samplePos += result.Length;

            if (AudioRead != null)
            {
                audioReadArgs.source = this;
                audioReadArgs.buffer = result;
                AudioRead(this, audioReadArgs);
            }

            return(result.Length);
        }
        private bool IsFilled(MixingBuffer buf)
        {
            bool res = true;

            for (int i = 0; i < buf.filled.Length; i++)
            {
                res &= buf.filled[i] || !this.playing[i];
            }
            return(res);
        }
示例#6
0
 public void Flush()
 {
     if (mixbuff != null)
     {
         if (mixbuff.source[iSource].Length < mixbuff.source[iSource].Size)
         {
             AudioSamples.MemSet(mixbuff.source[iSource].Bytes, 0, mixbuff.source[iSource].Length * Settings.PCM.BlockAlign, (mixbuff.source[iSource].Size - mixbuff.source[iSource].Length) * Settings.PCM.BlockAlign);
         }
         mixer.UnlockEmptyBuffer(mixbuff, iSource, volume);
         mixbuff = null;
     }
 }
 internal void UnlockFilledBuffer(MixingBuffer mixbuff)
 {
     lock (this)
     {
         //Trace.WriteLine(string.Format("UnockFilledBuffer: 0.0: {0} {1}; 0.1: {2} {3}; 1.0: {4} {5}; 1.1: {6} {7};",
         //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1],
         //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1]));
         for (int i = 0; i < mixbuff.filled.Length; i++)
         {
             mixbuff.filled[i] = false;
         }
         Monitor.PulseAll(this);
     }
 }
        internal void UnlockEmptyBuffer(MixingBuffer mixbuff, int iSource, float volume)
        {
            lock (this)
            {
                //Trace.WriteLine(string.Format("UnlockEmptyBuffer{8}< 0.0: {0} {1}; 0.1: {2} {3}; 1.0: {4} {5}; 1.1: {6} {7};",
                //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1],
                //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1], iSource));

                mixbuff.volume[iSource] = volume;
                mixbuff.filled[iSource] = true;

                //Trace.WriteLine(string.Format("UnlockEmptyBuffer{8}> 0.0: {0} {1}; 0.1: {2} {3}; 1.0: {4} {5}; 1.1: {6} {7};",
                //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1],
                //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1], iSource));

                Monitor.PulseAll(this);
            }
        }
示例#9
0
        public int Read(AudioBuffer result, int maxLength)
        {
            if (maxLength > (BufferSize - mixoffs) || maxLength < 0)
                maxLength = (BufferSize - mixoffs);

            result.Prepare(maxLength);

            if (mixbuff == null)
                mixbuff = LockFilledBuffer();

            float sumVolume = 0.0f;
            for (int iSource = 0; iSource < mixbuff.source.Length; iSource++)
                if (mixbuff.filled[iSource])
                    sumVolume += mixbuff.volume[iSource];
            for (int iSource = 0; iSource < mixbuff.source.Length; iSource++)
                volume[iSource] = mixbuff.filled[iSource] ? mixbuff.volume[iSource] / Math.Max(1.0f, sumVolume) : 0.0f;
            for (int iSmp = 0; iSmp < result.Length; iSmp++)
            {
                for (int iChan = 0; iChan < result.PCM.ChannelCount; iChan++)
                {
                    float sample = 0.0f;
                    for (int iSource = 0; iSource < mixbuff.source.Length; iSource++)
                        sample += mixbuff.source[iSource].Float[mixoffs + iSmp, iChan] * volume[iSource];
                    result.Float[iSmp, iChan] = sample;
                }
            }
            mixoffs += result.Length;
            if (mixoffs == BufferSize)
            {
                UnlockFilledBuffer(mixbuff);
                mixbuff = null;
                mixoffs = 0;
            }
            samplePos += result.Length;

            if (AudioRead != null)
            {
                audioReadArgs.source = this;
                audioReadArgs.buffer = result;
                AudioRead(this, audioReadArgs);
            }

            return result.Length;
        }
示例#10
0
        internal void UnlockEmptyBuffer(MixingBuffer mixbuff, int iSource, float volume)
        {
            lock (this)
            {
                //Trace.WriteLine(string.Format("UnlockEmptyBuffer{8}< 0.0: {0} {1}; 0.1: {2} {3}; 1.0: {4} {5}; 1.1: {6} {7};",
                //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1],
                //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1], iSource));

                mixbuff.volume[iSource] = volume;
                mixbuff.filled[iSource] = true;

                //Trace.WriteLine(string.Format("UnlockEmptyBuffer{8}> 0.0: {0} {1}; 0.1: {2} {3}; 1.0: {4} {5}; 1.1: {6} {7};",
                //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1],
                //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1], iSource));

                Monitor.PulseAll(this);
            }
        }
示例#11
0
 internal void UnlockFilledBuffer(MixingBuffer mixbuff)
 {
     lock (this)
     {
         //Trace.WriteLine(string.Format("UnockFilledBuffer: 0.0: {0} {1}; 0.1: {2} {3}; 1.0: {4} {5}; 1.1: {6} {7};",
         //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1],
         //    buf0.playing[0], buf0.filled[0], buf0.playing[1], buf0.filled[1]));
         for (int i = 0; i < mixbuff.filled.Length; i++)
             mixbuff.filled[i] = false;
         Monitor.PulseAll(this);
     }
 }
示例#12
0
 private bool IsFilled(MixingBuffer buf)
 {
     bool res = true;
     for (int i = 0; i < buf.filled.Length; i++)
         res &= buf.filled[i] || !this.playing[i];
     return res;
 }
示例#13
0
 public void Flush()
 {
     if (mixbuff != null)
     {
         if (mixbuff.source[iSource].Length < mixbuff.source[iSource].Size)
             AudioSamples.MemSet(mixbuff.source[iSource].Bytes, 0, mixbuff.source[iSource].Length * PCM.BlockAlign, (mixbuff.source[iSource].Size - mixbuff.source[iSource].Length) * PCM.BlockAlign);
         mixer.UnlockEmptyBuffer(mixbuff, iSource, volume);
         mixbuff = null;
     }
 }
示例#14
0
        public void Write(AudioBuffer buff)
        {
            int bs = PCM.BlockAlign;
            int buff_offs = 0;

            while (buff_offs < buff.Length)
            {
                if (mixbuff == null)
                {
                    mixbuff = mixer.LockEmptyBuffer(iSource);
                    mixbuff.source[iSource].Prepare(-1);
                    mixbuff.source[iSource].Length = 0;
                }

                int chunk = Math.Min(buff.Length - buff_offs, mixbuff.source[iSource].Size - mixbuff.source[iSource].Length);
                Buffer.BlockCopy(buff.Float, buff_offs * bs, mixbuff.source[iSource].Float, mixbuff.source[iSource].Length * bs, chunk * bs);
                mixbuff.source[iSource].Length += chunk;
                buff_offs += chunk;

                if (mixbuff.source[iSource].Length == mixbuff.source[iSource].Size)
                {
                    mixer.UnlockEmptyBuffer(mixbuff, iSource, volume);
                    mixbuff = null;
                }
            }

            samplePos += buff.Length;
        }