Beispiel #1
0
        public AudioSampleBuffer Update(Spread <AudioSampleBuffer> inputs, Spread <int> outputMap)
        {
            var arrayInputs = inputs?.ToList();
            var arrayMap    = outputMap?.ToArray();

            HasChanges = !AudioEngine.SequenceEquals(Inputs, arrayInputs) ||
                         !AudioEngine.ArrayEquals(OutputMap, arrayMap);

            Inputs    = arrayInputs;
            OutputMap = arrayMap;

            if (HasChanges || HotSwapped)
            {
                AudioEngine.Log($"AudioMixer: configuration changed");

                if (IsValid())
                {
                    processor = new AudioMixerProcessor(Inputs, OutputMap);
                    output    = processor.Build();
                }
                else
                {
                    output?.Dispose();
                    output = null;
                }

                HotSwapped = false;
            }

            return(output);
        }
Beispiel #2
0
        public AudioSampleBuffer Update(AudioSampleBuffer input, int fftLength, out Spread <float> spread)
        {
            bool hasChanges = fftLength != FftLength;

            FftLength = fftLength;
            Input     = input;
            if (hasChanges)
            {
                processor?.Dispose();
                processor = null;

                if (IsPowerOfTwo(fftLength))
                {
                    processor       = new FftProcessor(fftLength);
                    processor.Input = input;

                    if (input != null)
                    {
                        Output = processor.Build();
                    }
                }
            }

            if (processor != null)
            {
                processor.Input = input;
            }

            spread = processor?.Spread ?? Spread <float> .Empty;
            return(Output);
        }
Beispiel #3
0
 public AudioSplitBufferProcessor(AudioSampleBuffer input, int inputChannels, int outputChannels,
                                  int channels, CircularSampleBuffer buffer, CircularSampleBuffer[] buffersMapped, float[] tempBuffer)
 {
     this.input          = input;
     this.inputChannels  = inputChannels;
     this.outputChannels = outputChannels;
     this.channels       = channels;
     this.buffer         = buffer;
     this.buffersMapped  = buffersMapped;
     this.tempBuffer     = tempBuffer;
 }
Beispiel #4
0
        public AudioSampleBuffer Update(
            bool reset,
            AudioSampleBuffer input,
            Func <IFrameClock, TState> create,
            Func <TState, AudioSampleAccessor, TState> update,
            int outputChannels = 1,
            int oversample     = 1)
        {
            if (reset || state == null)
            {
                state = create(sampleClock);
                AudioEngine.Log($"AudioSampleLoop: Created {state}");
            }

            hasChanges = reset != this.reset ||
                         input != this.input ||
                         outputChannels != this.outputChannels ||
                         oversample != oversampling;

            this.reset          = reset;
            this.input          = input;
            createFunc          = create;
            updateFunc          = update;
            this.outputChannels = outputChannels;
            oversampling        = oversample;

            if (hasChanges || reset || HotSwapped)
            {
                AudioEngine.Log(
                    $"AudioSampleLoop({state}) configuration changed outChannels={outputChannels}, oversampling={oversample}");

                if (IsValid())
                {
                    processor = new AudioSampleLoopProcessor(state, input, sampleClock, update, outputChannels,
                                                             oversample);
                    output = processor.Build();
                }
                else
                {
                    output?.Dispose();
                    output = null;
                }

                HotSwapped = false;
            }

            processor.updateFunc = update;

            return(output);
        }
Beispiel #5
0
 public AudioSampleLoopProcessor(TState state, AudioSampleBuffer input, AudioSampleFrameClock sampleClock,
                                 Func <TState, AudioSampleAccessor, TState> updateFunc, int outputChannels, int oversampling)
 {
     this.state          = state;
     this.input          = input;
     this.updateFunc     = updateFunc;
     this.outputChannels = outputChannels;
     this.oversampling   = oversampling;
     this.sampleClock    = sampleClock;
     if (oversampling > 1)
     {
         BuildOversampling();
     }
 }
Beispiel #6
0
            public AudioMixerProcessor(List <AudioSampleBuffer> inputs, int[] outputMap)
            {
                this.inputs    = inputs;
                this.outputMap = outputMap;
                this.inputs    = inputs.Select(input =>
                {
                    if (input != null)
                    {
                        return(input);
                    }

                    return(AudioSampleBuffer.Silence());
                }).ToList();
            }
Beispiel #7
0
        public AudioSampleBuffer Update(float input)
        {
            Input = input;

            processor.Input = input;
            if (output == null)
            {
                output = new AudioSampleBuffer(WaveOutput.SingleChannelFormat)
                {
                    Processor = processor
                };
            }

            return(output);
        }
Beispiel #8
0
        public Spread <AudioSampleBuffer> Update(AudioSampleBuffer input, Spread <int> channelMap)
        {
            var arrayMap = channelMap?.ToArray();

            hasChanges = input != this.input || !AudioEngine.ArrayEquals(this.channelMap, arrayMap);

            this.input      = input;
            this.channelMap = arrayMap;

            if (hasChanges)
            {
                AudioEngine.Log($"AudioSplitter configuration changed!");

                if (IsValid())
                {
                    var outputsBefore = outputs?.Length ?? 0;

                    Build();

                    // Send nulls to update connected pins
                    var fillOutputs = outputsBefore - (outputs?.Length ?? 0);
                    if (fillOutputs > 0)
                    {
                        var builder = output.ToBuilder();
                        for (int i = 0; i < fillOutputs; i++)
                        {
                            builder.Add(null);
                        }

                        output = builder.ToSpread();
                    }
                }
                else
                {
                    foreach (var buffer in outputs)
                    {
                        buffer.Dispose();
                    }

                    // Send nulls to update connected pins
                    var results = outputs.Select(o => (AudioSampleBuffer)null);
                    return(results.ToSpread());
                }
            }

            return(output);
        }
Beispiel #9
0
        public void Update(WaveOutputDevice device, AudioSampleBuffer input, out string status,
                           out WaveFormat waveFormat, out int latency, out float cpuUsage,
                           out int bufferUnderRuns, int sampleRate = 44100,
                           int driverLatency = 200, int internalLatency = 300, int bufferSize = 512, bool reset = false)
        {
            bool hasDeviceChanged = device?.Value != Device?.Value ||
                                    sampleRate != SampleRate ||
                                    driverLatency != DriverLatency ||
                                    bufferSize != BufferSize ||
                                    reset;

            Device          = device;
            Input           = input;
            SampleRate      = sampleRate;
            InternalLatency = internalLatency;
            DriverLatency   = driverLatency;
            BufferSize      = bufferSize;

            if (hasDeviceChanged)
            {
                processor?.Dispose();
                processor = null;

                if (waveOut != null)
                {
                    AudioEngine.Log("Stopping WaveOut...");
                    waveOut.Stop();
                    waveOut.Dispose();
                }
            }

            if (processor == null)
            {
                processor = new AudioThread.AudioThreadProcessor(BufferSize);
            }

            processor.EnsureThreadIsRunning();
            processor.RequestedLatency = InternalLatency;

            if (hasDeviceChanged)
            {
                InternalFormat      = WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, 2);
                SingleChannelFormat = WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, 1);

                processor.WaveFormat = InternalFormat;

                if (device != null)
                {
                    AudioEngine.Log(
                        $"WaveOutput: Configuration changed, device={device.Value}, sampleRate={sampleRate}, latency={InternalLatency} {HotSwapped} ");
                    try
                    {
                        waveOut = ((IWaveOutputFactory)device.Tag).Create(DriverLatency);
                        var wave16 = new SampleToWaveProvider16(processor);
                        waveOut.Init(wave16);
                        waveOut.Play();
                        AudioEngine.Log("WaveOutput: Started");
                        OutputFormat = wave16.WaveFormat;
                    }
                    catch (Exception e)
                    {
                        AudioEngine.Log(e);
                        waveOut = null;
                    }
                }
            }

            processor.Input = Input;

            status = waveOut != null?waveOut.PlaybackState.ToString() : "Uninitialized";

            waveFormat      = OutputFormat;
            latency         = processor.Latency;
            cpuUsage        = processor.CpuUsage;
            bufferUnderRuns = processor.BufferUnderRuns;
        }
Beispiel #10
0
        public AudioSampleBuffer Update(WaveInputDevice device, out string status,
                                        out WaveFormat waveFormat, out int latency, out float cpuUsage, out int bufferUnderRuns,
                                        int driverLatency = 150, int internalLatency = 8, int bufferSize = 512, bool reset = false)
        {
            bool hasDeviceChanged = Device?.Value != device?.Value ||
                                    DriverLatency != driverLatency ||
                                    BufferSize != bufferSize ||
                                    reset;

            Device          = device;
            DriverLatency   = driverLatency;
            InternalLatency = internalLatency;
            BufferSize      = bufferSize;

            if (hasDeviceChanged)
            {
                processor?.Dispose();
                processor = null;

                if (waveIn != null)
                {
                    AudioEngine.Log("Stopping WaveIn...");
                    waveIn.StopRecording();
                    waveIn.Dispose();
                }
            }


            if (processor == null)
            {
                processor = new AudioThread.AudioThreadProcessor(bufferSize);
            }

            processor.EnsureThreadIsRunning();
            processor.RequestedLatency = internalLatency;

            if (hasDeviceChanged)
            {
                if (device != null)
                {
                    AudioEngine.Log(
                        $"WaveInput: Configuration changed, device={device.Value}, requested latency={DriverLatency}");
                    try
                    {
                        waveIn       = ((IWaveInputFactory)device.Tag).Create(DriverLatency);
                        bufferedWave = new BufferedWaveProvider(waveIn.WaveFormat);
                        bufferedWave.DiscardOnBufferOverflow = true;
                        sampleProvider       = new WaveToSampleProvider(bufferedWave);
                        OutputFormat         = sampleProvider.WaveFormat;
                        processor.WaveFormat = OutputFormat;
                        processor.Input      = sampleProvider;

                        waveIn.DataAvailable += (s, a) => { bufferedWave.AddSamples(a.Buffer, 0, a.BytesRecorded); };
                        waveIn.StartRecording();
                        AudioEngine.Log("WaveInput: Started");

                        output = new AudioSampleBuffer(OutputFormat)
                        {
                            Processor = processor
                        };
                    }
                    catch (Exception e)
                    {
                        AudioEngine.Log(e);
                        waveIn = null;
                    }
                }
            }


            status          = waveIn != null ? "Recording" : "Uninitialized";
            waveFormat      = OutputFormat;
            latency         = processor.Latency;
            cpuUsage        = processor.CpuUsage;
            bufferUnderRuns = processor.BufferUnderRuns;
            return(output);
        }
Beispiel #11
0
        public AudioSampleBuffer Update(AudioSampleBuffer input, out int latency,
                                        out float cpuUsage, out int bufferUnderRuns, bool runWithoutOutput, int internalLatency = 100,
                                        int bufferSize = 512,
                                        bool reset     = false)
        {
            bool hasChanges = Input != input ||
                              BufferSize != bufferSize ||
                              InternalLatency != internalLatency ||
                              RunWithoutOutput != runWithoutOutput ||
                              reset;

            Input            = input;
            InternalLatency  = internalLatency;
            BufferSize       = bufferSize;
            RunWithoutOutput = runWithoutOutput;

            if (reset || BufferSize != bufferSize)
            {
                processor?.Dispose();
                processor = null;
            }

            if (processor == null)
            {
                processor = new AudioThreadProcessor(BufferSize);
            }

            processor.EnsureThreadIsRunning();

            if (hasChanges)
            {
                processor.Input            = input;
                processor.RequestedLatency = InternalLatency;
                processor.RunWithoutOutput = RunWithoutOutput;
                if (input != null)
                {
                    processor.Input      = input;
                    processor.WaveFormat = input.WaveFormat;
                    if (!RunWithoutOutput)
                    {
                        Output = new AudioSampleBuffer(input.WaveFormat)
                        {
                            Processor = processor
                        };
                    }
                    else
                    {
                        Output = null;
                    }
                }
                else
                {
                    processor.ClearBuffer();
                }
            }

            latency         = processor.Latency;
            bufferUnderRuns = processor.BufferUnderRuns;
            cpuUsage        = processor.CpuUsage;
            return(Output);
        }