Ejemplo n.º 1
0
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Oscillator tmp;

            OscillatorPanel.Children.Clear();
            mixer.Oscillators.Clear();
            for (int i = 0; i < NUM_GENERATORS; i++)
            {
                tmp = new Oscillator();
                OscillatorPanel.Children.Add(tmp);
                mixer.Oscillators.Add(tmp);
            }
        }
Ejemplo n.º 2
0
 public OscillatorData(Oscillator osc, short[] data)
 {
     oscillator = osc;
     sampleData = data;
 }
Ejemplo n.º 3
0
        public short[] GenerateOscillatorSampleData(Oscillator osc)
        {
            // Creates a looping buffer based on the params given
            // Fill the buffer with whatever waveform at the specified frequency
            int numSamples = Convert.ToInt32(bufferDurationSeconds * SAMPLE_RATE);

            short[] sampleData = new short[numSamples];
            double  frequency  = osc.Frequency;
            int     amplitude  = osc.Amplitude;
            double  angle      = (Math.PI * 2 * frequency) / (SAMPLE_RATE * CHANNELS);

            switch (osc.WaveType)
            {
            case WaveType.Sine:
            {
                for (int i = 0; i < numSamples; i++)
                {
                    // Generate a sine wave in both channels.
                    sampleData[i] = Convert.ToInt16(amplitude * Math.Sin(angle * i));
                }
            }
            break;

            case WaveType.Square:
            {
                for (int i = 0; i < numSamples; i++)
                {
                    // Generate a square wave in both channels.
                    if (Math.Sin(angle * i) > 0)
                    {
                        sampleData[i] = Convert.ToInt16(amplitude);
                    }
                    else
                    {
                        sampleData[i] = Convert.ToInt16(-amplitude);
                    }
                    //sampleData[i] = Convert.ToInt16(maxAmplitude * Math.Floor(Math.Sin(angle * i)));
                }
            }
            break;

            case WaveType.Sawtooth:
            {
                int   samplesPerPeriod = Convert.ToInt32(SAMPLE_RATE / (frequency / CHANNELS));
                short sampleStep       = Convert.ToInt16((amplitude * 2) / samplesPerPeriod);
                short tempSample       = 0;

                int i = 0;
                int totalSamplesWritten = 0;
                while (totalSamplesWritten < numSamples)
                {
                    tempSample = (short)-amplitude;
                    for (i = 0; i < samplesPerPeriod && totalSamplesWritten < numSamples; i++)
                    {
                        tempSample += sampleStep;
                        sampleData[totalSamplesWritten] = tempSample;

                        totalSamplesWritten++;
                    }
                }
            }
            break;

            case WaveType.Noise:
            {
                Random rnd = new Random();
                for (int i = 0; i < numSamples; i++)
                {
                    sampleData[i] = Convert.ToInt16(rnd.Next(-amplitude, amplitude));
                }
            }
            break;
            }
            return(sampleData);
        }