Example #1
0
        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();
        }
Example #2
0
        private void MasterPlayButton_Click(object sender, EventArgs e)
        {
            /* Prepare tracks for left and right */
            int leftCount = 0, rightCount = 0;

            short[] rawLeft  = null;
            short[] rawRight = null;
            foreach (Track track in song.Tracks)
            {
                if (track.speakerSide())
                {
                    leftCount++;
                    if (rawLeft != null)
                    {
                        if (rawLeft.Length < track.getLength())
                        {
                            short[] tempLeft = new short[track.getLength()];
                            track.getData().CopyTo(tempLeft, 0);
                            for (int i = 0; i < rawLeft.Length; i++)
                            {
                                tempLeft[i] += rawLeft[i];
                            }
                            rawLeft = tempLeft;
                        }
                        else
                        {
                            for (int i = 0; i < track.getLength(); i++)
                            {
                                rawLeft[i] += track.getData()[i];
                            }
                        }
                    }
                    else
                    {
                        rawLeft = new short[track.getLength()];
                        track.getData().CopyTo(rawLeft, 0);
                    }
                }
                else
                {
                    rightCount++;
                    if (rawRight != null)
                    {
                        if (rawRight.Length < track.getLength())
                        {
                            short[] tempRight = new short[track.getLength()];
                            track.getData().CopyTo(tempRight, 0);
                            for (int i = 0; i < rawRight.Length; i++)
                            {
                                tempRight[i] += rawRight[i];
                            }
                            rawRight = tempRight;
                        }
                        else
                        {
                            for (int i = 0; i < track.getLength(); i++)
                            {
                                rawRight[i] += track.getData()[i];
                            }
                        }
                    }
                    else
                    {
                        rawRight = new short[track.getLength()];
                        track.getData().CopyTo(rawRight, 0);
                    }
                }
            }
            /* Tell the .wav which tracks we are using */
            int trackNum = 0;

            if (leftCount == 0 && rightCount == 0)
            {
                return;//neither track
            }
            else if (leftCount > 0 && rightCount > 0)
            {
                trackNum = 2;//use both
                /* Make sure the two sides are of equal length */
                if (rawLeft.Length != rawRight.Length)
                {
                    //return;
                }
            }
            else if (leftCount > 0)
            {
                trackNum = 0;//left only
            }
            else
            {
                trackNum = 1;//right only
            }
            /* Make the .wav */
            string        filePath = Directory.GetCurrentDirectory() + "\\song.wav";
            WaveGenerator wave;

            /* Construct the .wav as mono or stereo as appropriate */
            if (trackNum < 2)
            {
                wave = new WaveGenerator(false);
            }
            else
            {
                wave = new WaveGenerator(true);
            }
            wave.makeSong(rawLeft, rawRight, trackNum);
            wave.Save(filePath);
            SoundPlayer player = new SoundPlayer(filePath);

            player.Play();
        }
Example #3
0
        private void AddNoteButton_Click(object sender, EventArgs 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
                          );

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

            /* Add the note to the correct track */
            song.AddNote(wave.getData(), TrackIndex[0]);
        }