예제 #1
0
    /// Internal
    private void init(int queue_length, int sample_rate)
    {
        osc1 = new Phaser();
        osc2 = new Phaser();
        lfo  = new Phaser();
        fenv = new Phaser();
        aenv = new ADSR();

        note_is_on = false;

        for (int i = 0; i < 128; i++)
        { // 128 midi notes
            freqtab[i] = midi2freq(i % 12, i / 12 - 2);
        }

        this.sample_rate = sample_rate;

        filter1 = new MoogFilter(sample_rate);
        filter2 = new MoogFilter(sample_rate);
#if LAZZARINI_FILTER
        filter1Laz = new MoogFilter_Lazzarini(sample_rate);
        filter2Laz = new MoogFilter_Lazzarini(sample_rate);
#endif

        queue = new EventQueue(QueueCapacity);

        update_params();

        Reset();
    }
예제 #2
0
            public void Play(int voice, string frequency)
            {
                var ins = new List <Instruction> {
                    new Instruction {
                        code = "TRG", value = (char)(voice + 0x30) + "0000"
                    },                                                                                          //turn off the voice
                    new Instruction {
                        code = "VWF", value = (char)(voice + 0x30) + waveform.ToString("D4")
                    },                                                                                          //set desired waveform
                    new Instruction {
                        code = "VFQ", value = (char)(voice + 0x30) + frequency
                    },                                                                                          //set voice frequency
                    new Instruction {
                        code = "ENV", value = (char)(voice + 0x30) + ADSR.ToString()
                    },                                                                                          //set envelope
                };

                if (waveform == 2)
                {
                    ins.Add(
                        new Instruction {
                        code = "VPW", value = (char)(voice + 0x30) + pulsewidth.ToString("D4")
                    });                                                                                             //set pulsewidth if square wave selected
                }
                if (filter.EN)
                {
                    ins.Add(new Instruction {
                        code = "FFQ", value = filter.frequency.ToString()
                    });                                                                                             //set filter frequnecy
                    ins.Add(new Instruction {
                        code = "FRS", value = filter.resonance.ToString()
                    });                                                                                             //set resonance level
                    ins.Add(new Instruction {
                        code = "VFL", value = (char)(voice + 0x30) + "0001"
                    });                                                                                             //enable filter on chosen voice
                    ins.Add(new Instruction {
                        code = "FMO", value = "000" + filter.mode.ToString()
                    });                                                                                             //set filter mode
                }
                else
                {
                    ins.Add(new Instruction {
                        code = "VFL", value = (char)(voice + 0x30) + "0000"
                    });
                }

                ins.Add(new Instruction {
                    code = "TRG", value = (char)(voice + 0x30) + "0001"
                });                                                                                                 //finally, set the Gate bit of the voice con register

                foreach (var command in ins)
                {
                    SendPacket(command.code, command.value);
                    Thread.Sleep(3);//doesn't work
                    //for (int i = 0; i < 9000000; i++) ;
                }
            }
예제 #3
0
        public Audio(Instrument instrument)
        {
            Cancel   = false;
            Position = 0;

            Attack  = (0xFF - instrument.Attack) / (float)0xFF;
            Decay   = instrument.Decay / (float)0xFF;
            Sustain = instrument.Sustain / (float)0xFF;
            Release = instrument.Release / (float)0xFF;

            Envelope = 0;
            State    = ADSR.Attack;
        }
예제 #4
0
    private void Start()
    {
        frequencies = new float[] { 220.0f, 233.1f, 246.9f, 261.6f, 277.2f, 293.7f, 311.1f, 329.6f, 349.2f, 370.0f, 392.0f, 415.3f,
                                    440.0f, 466.2f, 493.9f, 523.3f, 554.4f, 587.3f, 622.3f, 659.3f, 698.5f, 740.0f, 784.0f, 830.6f, 880.0f,
                                    932.3f, 987.8f, 1047f, 1109f, 1175f, 1245f, 1319f, 1397f, 1480f, 1568f, 1661f, 1760f };

        adsr                = GetComponent <ADSR>();
        oscillator          = GetComponent <Oscillator>();
        rightController     = GameObject.Find("Controller (right)").GetComponent <HandInstrumentRight>();
        leftController      = GameObject.Find("Controller (left)").GetComponent <HandInstrumentLeft>();
        rightControllerHand = GameObject.Find("Controller (right)").GetComponent <Hand>();
        pitchLevels         = GameObject.Find("Pitch Levels").GetComponent <PitchRegionLayout>();
    }
예제 #5
0
 public void ConfigureADSR(ADSR adsr, OscillatorIndex oscillator, float newValue)
 {
     if (oscillator == OscillatorIndex.Primary)
     {
         if (adsr == ADSR.Attack)
         {
             primaryAttack = newValue;
         }
         else if (adsr == ADSR.Decay)
         {
             primaryDecay = newValue;
         }
         else if (adsr == ADSR.Sustain)
         {
             primarySustain = newValue;
         }
         else if (adsr == ADSR.Release)
         {
             primaryRelease = newValue;
         }
     }
     else if (oscillator == OscillatorIndex.Secondary)
     {
         if (adsr == ADSR.Attack)
         {
             secondaryAttack = newValue;
         }
         else if (adsr == ADSR.Decay)
         {
             secondaryDecay = newValue;
         }
         else if (adsr == ADSR.Sustain)
         {
             secondarySustain = newValue;
         }
         else if (adsr == ADSR.Release)
         {
             secondaryRelease = newValue;
         }
     }
 }
예제 #6
0
    static void Main(string[] args)
    {
        Session session = new Session();

        session.Open();

        int freq = 440; //Hz, Choose signal frequency, 440 Hz is audible and ok for speakers. Many tactors are closer to 150-250 Hz

        // We will begin by creating some basic oscillators, these are default amplitude 1.0 and infinite length of time
        Signal sin = new Sine(freq);     // Sine wave
        Signal squ = new Square(freq);   // Square wave
        Signal saw = new Saw(freq);      // Saw wave
        Signal tri = new Triangle(freq); // Triangle wave
        // We can use pulse width modulation (PWM) to quickly create a repeating cue train with frequency (1Hz) and duty cycle (0.3)
        Signal pwm = new Pwm(1, 0.3);

        // Now we can pair those oscillators with an envelope to give them shape

        // This is a basic envelope that specifies amplitude (0.9), and duration (0.5 sec)
        Signal bas = new Envelope(0.9, 0.5);
        // This is an attack (1 sec), sustain (3 sec), release (1 sec) envelope. The sustain amplitude is 1.0.
        Signal asr = new ASR(1, 2, 1, 1.0);
        // This adds one more part to the above envelope. Attack (1 sec, to amplitude 1.0), decay (2 sec),
        // sustain (3 sec, amplitude 0.8), release (1 sec). Curves can be added here as well
        Signal adsr = new ADSR(1, 2, 3, 1, 1.0, 0.8);


        // Pairing these oscillators and envelopes can give us complex cues

        Signal sig1 = sin * bas;

        Signal sig2 = sin * pwm * adsr;

        // More information in sequencing these in time can be found in examples_sequences
        session.Play(0, sig2);
        Sleep(sig2.length);
        session.Stop(0);

        session.Dispose();
    }
예제 #7
0
        protected void UpdateEnvelope()
        {
            switch (State)
            {
            case ADSR.Attack:
                Envelope += 1 / (Attack * SAMPLE_RATE);
                if (Envelope >= 1)
                {
                    Envelope = 1;
                    State    = ADSR.Decay;
                }
                break;

            case ADSR.Decay:
                Envelope -= 1 / (Decay * SAMPLE_RATE);
                if (Envelope <= Sustain)
                {
                    Envelope = Sustain;
                    State    = ADSR.Sustain;
                }
                break;

            case ADSR.Sustain:
                // the change to Release state is done externally
                break;

            case ADSR.Release:
                Envelope -= 1 / (Release * SAMPLE_RATE);
                if (Envelope <= 0)
                {
                    Envelope = 0;
                    Cancel   = true;
                }
                break;

            default: throw new Exception("ADSR State is invalid:" + State);
            }
        }
예제 #8
0
 // Use this for initialization
 void Start()
 {
     adsr = GetComponent(typeof(ADSR)) as ADSR;
 }