Exemple #1
0
    void ConfigureDSP()
    {
        AudioConfiguration audioConfig = AudioSettings.GetConfiguration();

        Debug.LogFormat("BufferSize={0} SampleRate={1}", audioConfig.dspBufferSize, audioConfig.sampleRate);
        _MicrophoneClip = Microphone.Start(Microphone.devices[0], true, 1, audioConfig.sampleRate);
        Debug.LogFormat("Microphone Channels={0} Frequency={1} Samples={2} Ambisonic={3}", _MicrophoneClip.channels, _MicrophoneClip.frequency, _MicrophoneClip.samples, _MicrophoneClip.ambisonic);
        _MicrophoneBuffer    = new NativeArray <float>(_MicrophoneClip.samples, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
        _MicrophoneDataArray = new float[_MicrophoneClip.samples];

        _Graph  = DSPGraph.Create(SoundFormat.Stereo, 2, audioConfig.dspBufferSize, audioConfig.sampleRate);
        _Driver = new MyAudioDriver {
            Graph = _Graph
        };
        _OutputHandle = _Driver.AttachToDefaultOutput();

        // create graph structure
        using (var block = _Graph.CreateCommandBlock())
        {
            //
            // create nodes
            //
            _Microphone = block.CreateDSPNode <MicrophoneNode.Parameters, MicrophoneNode.Providers, MicrophoneNode>();
            block.AddOutletPort(_Microphone, 1, SoundFormat.Mono);

            _Scope = block.CreateDSPNode <ScopeNode.Parameters, ScopeNode.Providers, ScopeNode>();
            block.AddInletPort(_Scope, 1, SoundFormat.Mono);

            _Spectrum = block.CreateDSPNode <SpectrumNode.Parameters, SpectrumNode.Providers, SpectrumNode>();
            block.AddInletPort(_Spectrum, 1, SoundFormat.Mono);

            _MonoToStereo = block.CreateDSPNode <MonoToStereoNode.Parameters, MonoToStereoNode.Providers, MonoToStereoNode>();
            block.AddInletPort(_MonoToStereo, 1, SoundFormat.Mono); // left
            block.AddInletPort(_MonoToStereo, 1, SoundFormat.Mono); // right
            block.AddOutletPort(_MonoToStereo, 2, SoundFormat.Stereo);

            //
            // connect nodes
            //
            block.Connect(_Microphone, 0, _MonoToStereo, 0);
            block.Connect(_Microphone, 0, _MonoToStereo, 1);

            block.Connect(_MonoToStereo, 0, _Graph.RootDSP, 0);

            block.Connect(_Microphone, 0, _Scope, 0);
            block.Connect(_Microphone, 0, _Spectrum, 0);

            //
            // set parameters
            //
            block.SetFloat <ScopeNode.Parameters, ScopeNode.Providers, ScopeNode>(_Scope, ScopeNode.Parameters.Time, 1f);
            block.SetFloat <ScopeNode.Parameters, ScopeNode.Providers, ScopeNode>(_Scope, ScopeNode.Parameters.TriggerTreshold, 0f);

            block.SetFloat <SpectrumNode.Parameters, SpectrumNode.Providers, SpectrumNode>(_Spectrum, SpectrumNode.Parameters.Window, (float)SpectrumNode.WindowType.Hamming);
        }

        _ScopeRenderer        = SpawnScopeRenderer(_Scope);
        _ScopeRenderer.Height = 5.0f;
        _ScopeRenderer.Offset = 0.0f;

        _SpectrumRenderer = SpawnSpectrumRenderer(_Spectrum);

        StartCoroutine(InitMicCoroutine());
    }
Exemple #2
0
    void Awake()
    {
        AudioConfiguration audioConfig = AudioSettings.GetConfiguration();

        Debug.LogFormat("BufferSize={0} SampleRate={1}", audioConfig.dspBufferSize, audioConfig.sampleRate);

        _Graph  = DSPGraph.Create(SoundFormat.Stereo, 2, audioConfig.dspBufferSize, audioConfig.sampleRate);
        _Driver = new MyAudioDriver {
            Graph = _Graph
        };
        _OutputHandle = _Driver.AttachToDefaultOutput();

        WidgetGraph.OnEnable();
        for (int i = WidgetGraph.nodes.Count - 1; i >= 0; --i)
        {
            WidgetGraph.nodes[i].Init();
        }
        for (int i = WidgetGraph.nodes.Count - 1; i >= 0; --i)
        {
            DSPNodeWidget dspNodeWidget = WidgetGraph.nodes[i] as DSPNodeWidget;
            if (dspNodeWidget == null)
            {
                continue;
            }
            foreach (var inputPort in dspNodeWidget.Ports)
            {
                if (!inputPort.IsInput)
                {
                    continue;
                }
                var dspInlet = dspNodeWidget.GetDSPPortAttribute(inputPort);
                if (dspInlet == null)
                {
                    continue;
                }

                var outputPorts = inputPort.GetConnections();
                foreach (var outputPort in outputPorts)
                {
                    if (!outputPort.IsOutput)
                    {
                        continue;
                    }
                    DSPNodeWidget dspNodeWidget2 = outputPort.node as DSPNodeWidget;
                    if (dspNodeWidget2 == null)
                    {
                        continue;
                    }
                    var dspOutlet = dspNodeWidget2.GetDSPPortAttribute(outputPort);
                    if (dspOutlet == null)
                    {
                        continue;
                    }

                    using (var block = DSPGraph.CreateCommandBlock())
                    {
                        block.Connect(dspNodeWidget2.DSPNode, dspOutlet.portIndex, dspNodeWidget.DSPNode, dspInlet.portIndex);
                    }
                }
            }
        }
    }
    void ConfigureDSP()
    {
        AudioConfiguration audioConfig = AudioSettings.GetConfiguration();

        Debug.LogFormat("BufferSize={0} SampleRate={1}", audioConfig.dspBufferSize, audioConfig.sampleRate);

        _Graph  = DSPGraph.Create(SoundFormat.Stereo, 2, audioConfig.dspBufferSize, audioConfig.sampleRate);
        _Driver = new MyAudioDriver {
            Graph = _Graph
        };
        _OutputHandle = _Driver.AttachToDefaultOutput();

        // create graph structure
        using (var block = _Graph.CreateCommandBlock())
        {
            //
            // create nodes
            //
            _Oscilator1 = CreateOscilator(block);
            _Oscilator2 = CreateOscilator(block);
            _Oscilator3 = CreateOscilator(block);
            _Oscilator4 = CreateOscilator(block);

            _ADSR1 = CreateADSR(block);
            _ADSR2 = CreateADSR(block);
            _ADSR3 = CreateADSR(block);
            _ADSR4 = CreateADSR(block);

            _VCA1 = CreateVCA(block);
            _VCA2 = CreateVCA(block);

            _Mixer3 = CreateMixer(block);
            _Mixer4 = CreateMixer(block);

            _Midi = CreateMidi(block);

            _Attenuator = CreateAttenuator(block);

            _MonoToStereo = CreateMonoToStereo(block);

            _Scope    = CreateMonoScope(block);
            _Spectrum = CreateSpectrum(block);

            //
            // connect nodes
            //
            block.Connect(_Midi, 0, _ADSR1, 0); // midi gate to adsr
            block.Connect(_Midi, 0, _ADSR2, 0);
            block.Connect(_Midi, 0, _ADSR3, 0);
            block.Connect(_Midi, 0, _ADSR4, 0);

            block.Connect(_Midi, 1, _Oscilator1, 1); // midi note to oscilator pitch
            block.Connect(_Midi, 1, _Oscilator2, 1);
            block.Connect(_Midi, 1, _Oscilator3, 1);
            block.Connect(_Midi, 1, _Oscilator4, 1);

            block.Connect(_Midi, 2, _Oscilator1, 2); // midi retrigger to oscilator reset phase
            block.Connect(_Midi, 2, _Oscilator2, 2);
            block.Connect(_Midi, 2, _Oscilator3, 2);
            block.Connect(_Midi, 2, _Oscilator4, 2);

            block.Connect(_ADSR1, 0, _VCA1, 0); // adsr gate to vca voltage
            block.Connect(_ADSR2, 0, _VCA2, 0);

            block.Connect(_Oscilator1, 0, _VCA1, 1); // oscilator out to vca in
            block.Connect(_Oscilator2, 0, _VCA2, 1);

            block.Connect(_VCA1, 0, _Oscilator3, 0); // vca out to oscilator fm
            block.Connect(_VCA2, 0, _Oscilator4, 0);

            block.Connect(_ADSR3, 0, _Mixer3, 1); // adsr gate to mixer cv
            block.Connect(_ADSR4, 0, _Mixer4, 1);

            block.Connect(_Oscilator3, 0, _Mixer3, 0); // oscilator out to mixer in
            block.Connect(_Oscilator4, 0, _Mixer4, 0);

            block.Connect(_Mixer3, 0, _Attenuator, 0); // mixer out to attenuator in
            block.Connect(_Mixer4, 0, _Attenuator, 0);

            block.Connect(_Attenuator, 0, _MonoToStereo, 0);    // attenuator out to monotostereo left
            block.Connect(_Attenuator, 0, _MonoToStereo, 1);    // attenuator out to monotostereo right

            block.Connect(_MonoToStereo, 0, _Graph.RootDSP, 0); // monotostereo out to output

            block.Connect(_Attenuator, 0, _Scope, 0);
            block.Connect(_Attenuator, 0, _Spectrum, 0);

            //
            // parameters
            //
            block.SetFloat <OscilatorNode.Parameters, OscilatorNode.Providers, OscilatorNode>(_Oscilator1, OscilatorNode.Parameters.Frequency, 130.813f);
            block.SetFloat <OscilatorNode.Parameters, OscilatorNode.Providers, OscilatorNode>(_Oscilator1, OscilatorNode.Parameters.Mode, (float)OscilatorNode.Mode.Sine);

            block.SetFloat <OscilatorNode.Parameters, OscilatorNode.Providers, OscilatorNode>(_Oscilator2, OscilatorNode.Parameters.Frequency, 130.813f);
            block.SetFloat <OscilatorNode.Parameters, OscilatorNode.Providers, OscilatorNode>(_Oscilator2, OscilatorNode.Parameters.Mode, (float)OscilatorNode.Mode.Sine);

            block.SetFloat <OscilatorNode.Parameters, OscilatorNode.Providers, OscilatorNode>(_Oscilator3, OscilatorNode.Parameters.Frequency, 261.626f);
            block.SetFloat <OscilatorNode.Parameters, OscilatorNode.Providers, OscilatorNode>(_Oscilator3, OscilatorNode.Parameters.Mode, (float)OscilatorNode.Mode.Sine);
            block.SetFloat <OscilatorNode.Parameters, OscilatorNode.Providers, OscilatorNode>(_Oscilator3, OscilatorNode.Parameters.FMMultiplier, 0.5f);

            block.SetFloat <OscilatorNode.Parameters, OscilatorNode.Providers, OscilatorNode>(_Oscilator4, OscilatorNode.Parameters.Frequency, 130.813f);
            block.SetFloat <OscilatorNode.Parameters, OscilatorNode.Providers, OscilatorNode>(_Oscilator4, OscilatorNode.Parameters.Mode, (float)OscilatorNode.Mode.Sine);
            block.SetFloat <OscilatorNode.Parameters, OscilatorNode.Providers, OscilatorNode>(_Oscilator4, OscilatorNode.Parameters.FMMultiplier, 0.4f);

            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR1, ADSRNode.Parameters.Attack, 0.1f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR1, ADSRNode.Parameters.Decay, 0.05f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR1, ADSRNode.Parameters.Sustain, 0.5f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR1, ADSRNode.Parameters.Release, 0.2f);

            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR2, ADSRNode.Parameters.Attack, 0.1f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR2, ADSRNode.Parameters.Decay, 0.05f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR2, ADSRNode.Parameters.Sustain, 0.5f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR2, ADSRNode.Parameters.Release, 0.2f);

            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR3, ADSRNode.Parameters.Attack, 0.05f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR3, ADSRNode.Parameters.Decay, 0.05f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR3, ADSRNode.Parameters.Sustain, 0.5f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR3, ADSRNode.Parameters.Release, 0.1f);

            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR4, ADSRNode.Parameters.Attack, 0.05f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR4, ADSRNode.Parameters.Decay, 0.05f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR4, ADSRNode.Parameters.Sustain, 0.5f);
            block.SetFloat <ADSRNode.Parameters, ADSRNode.Providers, ADSRNode>(_ADSR4, ADSRNode.Parameters.Release, 0.1f);

            block.SetFloat <VCANode.Parameters, VCANode.Providers, VCANode>(_VCA1, VCANode.Parameters.Multiplier, 1.0f);
            block.SetFloat <VCANode.Parameters, VCANode.Providers, VCANode>(_VCA2, VCANode.Parameters.Multiplier, 1.0f);

            block.SetFloat <AttenuatorNode.Parameters, AttenuatorNode.Providers, AttenuatorNode>(_Attenuator, AttenuatorNode.Parameters.Multiplier, 1.0f);

            block.SetFloat <ScopeNode.Parameters, ScopeNode.Providers, ScopeNode>(_Scope, ScopeNode.Parameters.Time, 0.05f);
            block.SetFloat <ScopeNode.Parameters, ScopeNode.Providers, ScopeNode>(_Scope, ScopeNode.Parameters.TriggerTreshold, 0f);

            block.SetFloat <SpectrumNode.Parameters, SpectrumNode.Providers, SpectrumNode>(_Spectrum, SpectrumNode.Parameters.Window, (float)SpectrumNode.WindowType.BlackmanHarris);
        }

        _ScopeRenderer        = SpawnScopeRenderer(_Scope);
        _ScopeRenderer.Height = 5.01f;
        _ScopeRenderer.Offset = 0f;

        _SpectrumRenderer = SpawnSpectrumRenderer(_Spectrum);
    }