void DoCacheFile()
        {
            if (FCacheFile)
            {
                var channels  = FSampleChannel.WaveFormat.Channels;
                var cacheSize = FReaderStream.Length / (4 * channels);

                Cache = new float[channels][];

                for (int i = 0; i < Cache.Length; i++)
                {
                    Cache[i] = new float[cacheSize];
                }

                long totalFloatsRead = 0;
                var  buffer          = new float[FSampleChannel.WaveFormat.AverageBytesPerSecond * 4 * channels];
                var  cacheIndex      = 0;
                FReaderStream.Position = 0;

                while (true)
                {
                    int floatsRead = FSampleChannel.Read(buffer, 0, buffer.Length);

                    totalFloatsRead += floatsRead;

                    bool finished = false;
                    for (int i = 0; i < floatsRead; i += channels)
                    {
                        for (int channel = 0; channel < channels; channel++)
                        {
                            Cache[channel][cacheIndex] = buffer[i + channel];
                        }

                        cacheIndex++;
                        if (cacheIndex >= cacheSize)
                        {
                            finished = true;
                            break;
                        }
                    }


                    if (finished)
                    {
                        //end of source
                        break;
                    }


                    if (totalFloatsRead > Int32.MaxValue)
                    {
                        throw new InvalidOperationException("WAV File cannot be greater than 2GB. Check that sourceProvider is not an endless stream.");
                    }
                }
            }
        }
Beispiel #2
0
        public int Read(float[] buffer, int offset, int count)
        {
            var numberOfInUseInputs = voiceInputs.Count(x => x.InUse);

            if (numberOfInUseInputs > 1)
            {
                blockTone.Frequency = 180;
                blockTone.Gain      = blockToneGain;
            }
            else
            {
                blockTone.Gain = 0;
            }

            if (doClickWhenAppropriate && numberOfInUseInputs == 0)
            {
                mixer.AddMixerInput(new ResourceSoundSampleProvider(Samples.Instance.Click)
                {
                    Gain = clickGain
                });
                doClickWhenAppropriate = false;
            }

            if (numberOfInUseInputs != lastNumberOfInUseInputs)
            {
                ReceivingCallsignsChanged?.Invoke(this, new TransceiverReceivingCallsignsChangedEventArgs(ID, voiceInputs.Select(x => x.Callsign).Where(x => !string.IsNullOrWhiteSpace(x)).ToList()));
            }
            lastNumberOfInUseInputs = numberOfInUseInputs;

            return(volume.Read(buffer, offset, count));
        }
        public int Read(float[] buffer, int offset, int sampleCount)
        {
            if (!_sound.IsPlaying)
            {
                return(0);
            }
            _volumeProvider.Volume = _sound.Mute ? 0 : _sound.Volume * _volume[_sound.Type];
            var read = _volumeProvider.Read(buffer, offset, sampleCount);

            if (read < sampleCount)
            {
                _reader.Position = 0;
                if (_sound.Looping)
                {
                    read = _volumeProvider.Read(buffer, offset, sampleCount);
                }
                else
                {
                    _sound.IsPlaying = false;
                }
            }
            return(read);
        }
Beispiel #4
0
        public void TestNAudio()
        {
            using (var reader = new WaveFileReader("Sound\\CAU1.wav"))
            {
                //WaveFormatConversionStream conv = new WaveFormatConversionStream(WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, 8000, 1, 8000, 1, 8), reader);

                var sp = new VolumeSampleProvider(reader.ToSampleProvider());
                sp.Volume = 0.5f;
                using (var writer = new WaveFileWriter("dv.wav", WaveFormat.CreateALawFormat(8000, 1)))
                {
                    int sampleCount = (int)reader.SampleCount;
                    var buff        = new float[sampleCount];
                    sp.Read(buff, 0, sampleCount);
                    writer.WriteSamples(buff, 0, sampleCount);
                    writer.Close();
                }
                reader.Close();
            }
        }
Beispiel #5
0
        public int Read(float[] buffer, int offset, int count)
        {
            //Read requested data from upstream
            var read = _upstream.Read(buffer, offset, count);

            //Write RMS of these samples into RMS buffer
            for (var i = 0; i < read; i++)
            {
                var v = buffer[offset + i];
                _rmsBuffer[_rmsWriteHead++ % _rmsBuffer.Length] = v * v;
            }

            //Calculate RMS over signal
            var rms = Math.Sqrt(_rmsBuffer.Sum() / _rmsBuffer.Length);

            Dbfs = 20 * Math.Log10(rms) + 3.0103;

            //Calculate direction to change gain
            double rate;

            if (Dbfs > _maxVolume)
            {
                rate = -Math.Abs(_downRate);
            }
            else if (Dbfs < _minVolume)
            {
                rate = _upRate;
            }
            else
            {
                return(read);
            }

            //Adjust gain
            var gain = _upstream.Volume + rate * _secondsPerSample * read;

            gain             = Math.Max(gain, _minGain);
            gain             = Math.Min(gain, _maxGain);
            _upstream.Volume = (float)gain;

            return(read);
        }
Beispiel #6
0
 public int Read(float[] buffer, int offset, int count)
 {
     return(volumeProvider.Read(buffer, offset, count));
 }