예제 #1
0
파일: Track.cs 프로젝트: NeutronStar/Muse
        public void PlayButton_Click(object sender, EventArgs e)
        {
            string        filePath = Directory.GetCurrentDirectory() + "\\track.wav";
            WaveGenerator wave     = new WaveGenerator(false);

            wave.makeTrack(data);
            wave.Save(filePath);
            SoundPlayer player = new SoundPlayer(filePath);

            player.Play();
        }
예제 #2
0
        private void button1_MouseClick_1(object sender, MouseEventArgs e)
        {
            WaveExampleType[] selection = new WaveExampleType[3];
            for (int i = 0; i < 3; i++)
            {
                selection[i] = WaveExampleType.SineWave;
            }
            RadioButton[][] buttons = new RadioButton[3][];
            for (int i = 0; i < 3; i++)
            {
                buttons[i] = new RadioButton[6];
            }
            buttons[0][0] = SineButton1; buttons[0][1] = SquareButton1; buttons[0][2] = SawtoothButton1; buttons[0][3] = TriangleButton1; buttons[0][4] = WhiteButton1; buttons[0][5] = NothingButton1;
            buttons[1][0] = SineButton2; buttons[1][1] = SquareButton2; buttons[1][2] = SawtoothButton2; buttons[1][3] = TriangleButton2; buttons[1][4] = WhiteButton2; buttons[1][5] = NothingButton2;
            buttons[2][0] = SineButton3; buttons[2][1] = SquareButton3; buttons[2][2] = SawtoothButton3; buttons[2][3] = TriangleButton3; buttons[2][4] = WhiteButton3; buttons[2][5] = NothingButton3;
            for (int i = 0; i < 3; i++)
            {
                if (buttons[i][0].Checked)
                {
                    selection[i] = WaveExampleType.SineWave;
                }
                else if (buttons[i][1].Checked)
                {
                    selection[i] = WaveExampleType.SquareWave;
                }
                else if (buttons[i][2].Checked)
                {
                    selection[i] = WaveExampleType.SawtoothWave;
                }
                else if (buttons[i][3].Checked)
                {
                    selection[i] = WaveExampleType.TriangleWave;
                }
                else if (buttons[i][4].Checked)
                {
                    selection[i] = WaveExampleType.WhiteNoise;
                }
                else if (buttons[i][5].Checked)
                {
                    selection[i] = WaveExampleType.Nothing;
                }
                else
                {
                    selection[i] = WaveExampleType.Nothing;
                }
            }

            /* Find the frequency of each note */
            double[]   frequency        = new double[3];
            TrackBar[] octaveSliders    = new TrackBar[3];
            TrackBar[] frequencySliders = new TrackBar[3];
            octaveSliders[0]    = OctaveSlider1; octaveSliders[1] = OctaveSlider2; octaveSliders[2] = OctaveSlider3;
            frequencySliders[0] = FrequencySlider1; frequencySliders[1] = FrequencySlider2; frequencySliders[2] = FrequencySlider3;
            for (int i = 0; i < 3; i++)
            {
                double power      = octaveSliders[i].Value - 3;
                double multiplier = Math.Pow(2, power);
                frequency[i] = Notes[frequencySliders[i].Value] * multiplier;
            }

            /* Find the amplitude of each note */
            int[]      volume        = new int[3];
            TrackBar[] volumeSliders = new TrackBar[3];
            volumeSliders[0] = VolumeSlider1; volumeSliders[1] = VolumeSlider2; volumeSliders[2] = VolumeSlider3;
            for (int i = 0; i < 3; i++)
            {
                volume[i] = volumeSliders[i].Value * (VolMax / 10);
            }

            /* Find the ADSR times */
            double[] ADSRtime     = new double[6];
            float    clipDuration = 0;

            foreach (System.Windows.Forms.Control control in this.DurationGroupBox.Controls)
            {
                System.Windows.Forms.RadioButton button = (System.Windows.Forms.RadioButton)control;
                if (button.Checked)
                {
                    clipDuration = float.Parse(button.Tag.ToString(), CultureInfo.InvariantCulture.NumberFormat) / (this.BPM / 60);
                }
            }
            ADSRtime[0] = 0;
            ADSRtime[1] = AttackTime.Value * (clipDuration / 1000) * 44100;
            ADSRtime[2] = DecayTime.Value * (clipDuration / 1000) * 44100;
            ADSRtime[3] = SustainTime.Value * (clipDuration / 1000) * 44100;
            ADSRtime[4] = ReleaseTime.Value * (clipDuration / 1000) * 44100;
            ADSRtime[5] = clipDuration * 44100;

            /* Find the ADSR amplitudes */
            double[] ADSRvolume = new double[4];
            ADSRvolume[0] = (float)AttackAmplitude.Value / 100;
            ADSRvolume[1] = (float)DecayAmplitude.Value / 100;
            ADSRvolume[2] = (float)SustainAmplitude.Value / 100;
            ADSRvolume[3] = (float)ReleaseAmplitude.Value / 100;

            string        filePath = Directory.GetCurrentDirectory() + "\\sound.wav";
            WaveGenerator wave     = new WaveGenerator(false);

            wave.makeNote(selection,
                          frequency,
                          volume,
                          clipDuration,
                          ADSRtime,
                          ADSRvolume
                          );
            wave.Save(filePath);

            /* Show what the final plot looks like */
            UpdateSoundWaveChart(wave.getNumSamples(), wave.getData());

            SoundPlayer player = new SoundPlayer(filePath);

            player.Play();
        }