using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.IO;
using NAudio;

namespace MusicalBox
{
    public partial class Form1 : Form
    {
        Form2 help;
        public Form1()
        {
            InitializeComponent();
           
        }

        private class Item
        {
            public string Name;

            public Item()
            {
                Name ="";
            }
        }

        private NAudio.Wave.BlockAlignReductionStream stream = null;

        private NAudio.Wave.DirectSoundOut output = null;
        
        DirectoryInfo dirTabs, dirRecorded;
        FileInfo[] filesTabs, filesRecorded;

        int position, length;

        string dirName, tabs;

        int tabRecordIsPressed, shift;

        private void Form1_Load(object sender, EventArgs e)
        {
            help = new Form2();
            help.Show();

            tabs = "";
            shift = 0;

            button2.Enabled = false;
            button3.Enabled = false;

            dirName = "Recorded";

            ShowTracks(dirName);

            toolTip1 = new ToolTip();
            toolTip1.AutoPopDelay = 9000;
            toolTip1.InitialDelay = 100;
            toolTip1.ReshowDelay = 500;
            toolTip1.ShowAlways = true;
            toolTip1.SetToolTip(this.button5, "Выбрать файл для воспроизведения вручную");
            toolTip1.SetToolTip(this.dirButton, "Выбрать каталог с музыкальными файлами");

            tabRecordIsPressed = 0;

            dirTabs = new DirectoryInfo("Tabs");
            filesTabs = dirTabs.GetFiles();
            foreach(FileInfo file in filesTabs)
                comboBox1.Items.Add(file.Name.Remove(file.Name.Length - 4,4));

            comboBox1.SelectedIndex = 0;

        }
        private void ShowTracks(string directory)
        {

            dataGridView1.Rows.Clear();

            dirRecorded = new DirectoryInfo(directory);
            filesRecorded = dirRecorded.GetFiles();

            int i = 0;

            foreach (FileInfo file in filesRecorded)
            {
                if (file.Name.Substring(file.Name.Length - 3, 3) == "wav")
                {
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[i].Cells[0].Value = filesRecorded[i].Name;
                    i++;
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            button2.Enabled = true;
            List<NAudio.Wave.WaveInCapabilities> sources = new List<NAudio.Wave.WaveInCapabilities>();

            for (int i = 0; i < NAudio.Wave.WaveIn.DeviceCount; i++)
            {
                sources.Add(NAudio.Wave.WaveIn.GetCapabilities(i));
            }
            deviceList.Items.Clear();

            foreach (var source in sources)
            {
                ListViewItem item = new ListViewItem(source.ProductName);
                item.SubItems.Add(new ListViewItem.ListViewSubItem(item, source.Channels.ToString()));
                deviceList.Items.Add(item);
            }

        }

        NAudio.Wave.WaveIn sourceStream = null;
        NAudio.Wave.DirectSoundOut waveOut = null;
        NAudio.Wave.WaveFileWriter waveWriter = null;

        private void button2_Click(object sender, EventArgs e)
        {
            if (deviceList.SelectedItems.Count == 0) return;

            button3.Enabled = true;
            tabRecord.Enabled = false;

            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Wave File (*.wav)|*.wav;";
            if (save.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

            int deviceNumber = deviceList.SelectedItems[0].Index;

            sourceStream = new NAudio.Wave.WaveIn();
            sourceStream.DeviceNumber = deviceNumber;
            sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);


            sourceStream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(sourceStream_DataAvailiable);
            waveWriter = new NAudio.Wave.WaveFileWriter(save.FileName, sourceStream.WaveFormat);

            sourceStream.StartRecording();
            label1.Visible = true;
            pictureBox2.Visible = true;
        }

        private void sourceStream_DataAvailiable(object sender, NAudio.Wave.WaveInEventArgs e)
        {
            if (waveWriter == null) return;
            waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
            waveWriter.Flush();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (waveOut != null)
            {
                waveOut.Stop();
                waveOut.Dispose();
                waveOut = null;
            }
            if (sourceStream != null)
            {
                sourceStream.StopRecording();
                sourceStream.Dispose();
                sourceStream = null;

            }
            if (waveWriter != null)
            {
                waveWriter.Dispose();
                waveWriter = null;
            }
            label1.Visible = false;
            pictureBox2.Visible = false;
            button3.Enabled = false;
            tabRecord.Enabled = true;


            filesRecorded = dirRecorded.GetFiles();
            dataGridView1.Rows.Clear();

            for (int i = 0; i < filesRecorded.Length; i++)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[i].Cells[0].Value = filesRecorded[i].Name;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button3_Click(sender, e);
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Audio File (*.mp3; *.wav)|*.mp3;*.wav;";
            if (open.ShowDialog() != DialogResult.OK) return;

            DisposeWave();

            try
            {

                if (open.FileName.EndsWith(".mp3"))
                {
                    NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(open.FileName));
                    stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
                }
                else if (open.FileName.EndsWith(".wav"))
                {
                    NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));
                    stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
                }
                else throw new InvalidOperationException("Not a correct audio file type.");
          

            output = new NAudio.Wave.DirectSoundOut();
            output.Init(stream);
            output.Play();

            PlayButton.Enabled = true;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (output != null)
            {
                if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Pause();
                else if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused) output.Play();
            }
        }

        private void PlyaButton_Click(object sender, EventArgs e)
        {
            if (output != null)
                if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused) output.Play();
            DisposeWave();
            if (dataGridView1.SelectedCells.Count != 0)
            {
                NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(filesRecorded[dataGridView1.CurrentCell.RowIndex].FullName));
                stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
                output = new NAudio.Wave.DirectSoundOut();
                output.Init(stream);
                output.Play();
            }
        }

        private void PauseButton_Click(object sender, EventArgs e)
        {
            if (output != null)
                if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Pause();
        }


        private void DisposeWave()
        {
            if (output != null)
            {
                if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Stop();
                output.Dispose();
                output = null;
            }
            if (stream != null)
            {
                stream.Dispose();
                stream = null;
            }
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            tabLabel.Text = tabLabel.Text.Trim(' ');
            string key="";

            System.Windows.Media.MediaPlayer[] p = new System.Windows.Media.MediaPlayer[36];
            for (int i = 0; i < 36; i++)
            {
                p[i] = new System.Windows.Media.MediaPlayer();
            }

            string dir = "Sounds_New";


            if (tabLabel.Text != "(end)" && tabRecordIsPressed % 2 == 0)
            if (position < tabLabel.Text.Length )
            {

                if (tabLabel.Text[position] == ' ' || tabLabel.Text[position] == '[' || tabLabel.Text[position] == '[')
                    position++;
                if (e.KeyChar == tabLabel.Text[position])
                {
                    string temp = tabLabel.Text;
                    if (tabLabel.Text[position + 1] != ' ')
                        if (tabLabel.Text[position + 2] != ' ')
                            temp = temp.Remove(position, 3);
                        else temp = temp.Remove(position, 2);
                    else
                        temp = temp.Remove(position, 1);
                    tabLabel.Text = temp;
                }
            }


            if (tabRecordIsPressed % 2 == 1)
            {
                if ((Control.ModifierKeys & Keys.Shift) != 0)
                {
                    tabs += " " + Char.ToLower(e.KeyChar);
                    if (tabs.Length > 16) shift+=2;
                    tabLabel.Text = tabs.Substring(shift);
                }
            }


            switch (e.KeyChar)
            {
                case '1':
                    {
                        key = Path.GetFullPath(dir) + "\\C-1.wav";
                        p[0].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\C-1.png");
                        p[0].Play();
                        noteLabel.Text = key.Substring(key.Length-7,3);

                    }
                    break;
                case '2':
                    {
                        key = Path.GetFullPath(dir) + "\\D-1.wav";
                        p[1].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\D-1.png");
                        p[1].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case '3':
                    {
                        key = Path.GetFullPath(dir) + "\\E-1.wav";
                        p[2].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\E-1.png");
                        p[2].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case '4':
                    {
                        key = Path.GetFullPath(dir) + "\\F-1.wav";
                        p[3].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\F-1.png");
                        p[3].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case '5':
                    {
                        key = Path.GetFullPath(dir) + "\\G-1.wav";
                        p[4].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\G-1.png");
                        p[4].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case '6':
                    {
                        key = Path.GetFullPath(dir) + "\\A-1.wav";
                        p[5].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\A-1.png");
                        p[5].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case '7':
                    {
                        key = Path.GetFullPath(dir) + "\\B-1.wav";
                        p[6].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\B-1.png");
                        p[6].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case '8':
                    {
                        key = Path.GetFullPath(dir) + "\\C-2.wav";
                        p[7].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\C-2.png");
                        p[7].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case '9':
                    {
                        key = Path.GetFullPath(dir) + "\\D-2.wav";
                        p[8].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\D-2.png");
                        p[8].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case '0':
                    {
                        key = Path.GetFullPath(dir) + "\\E-2.wav";
                        p[9].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\E-2.png");
                        p[9].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'q':
                    {
                        key = Path.GetFullPath(dir) + "\\F-2.wav";
                        p[10].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\F-2.png");
                        p[10].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'w':
                    {
                        key = Path.GetFullPath(dir) + "\\G-2.wav";
                        p[11].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\G-2.png");
                        p[11].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'e':
                    {
                        key = Path.GetFullPath(dir) + "\\A-2.wav";
                        p[12].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\A-2.png");
                        p[12].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'r':
                    {
                        key = Path.GetFullPath(dir) + "\\B-2.wav";
                        p[13].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\B-2.png");
                        p[13].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 't':
                    {
                        key = Path.GetFullPath(dir) + "\\C-3.wav";
                        p[14].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\C-3.png");
                        p[14].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'y':
                    {
                        key = Path.GetFullPath(dir) + "\\D-3.wav";
                        p[15].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\D-3.png");
                        p[15].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'u':
                    {
                        key = Path.GetFullPath(dir) + "\\E-3.wav";
                        p[16].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\E-3.png");
                        p[16].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'i':
                    {
                        key = Path.GetFullPath(dir) + "\\F-3.wav";
                        p[17].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\F-3.png");
                        p[17].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'o':
                    {
                        key = Path.GetFullPath(dir) + "\\G-3.wav";
                        p[18].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\G-3.png");
                        p[18].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'p':
                    {
                        key = Path.GetFullPath(dir) + "\\A-3.wav";
                        p[19].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\A-3.png");
                        p[19].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'a':
                    {
                        key = Path.GetFullPath(dir) + "\\B-3.wav";
                        p[20].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\B-3.png");
                        p[20].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 's':
                    {
                        key = Path.GetFullPath(dir) + "\\C-4.wav";
                        p[21].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\C-4.png");
                        p[21].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'd':
                    {
                        key = Path.GetFullPath(dir) + "\\D-4.wav";
                        p[22].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\D-4.png");
                        p[22].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'f':
                    {
                        key = Path.GetFullPath(dir) + "\\E-4.wav";
                        p[23].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\E-4.png");
                        p[23].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'g':
                    {
                        key = Path.GetFullPath(dir) + "\\F-4.wav";
                        p[24].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\F-4.png");
                        p[24].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'h':
                    {
                        key = Path.GetFullPath(dir) + "\\G-4.wav";
                        p[25].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\G-4.png");
                        p[25].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'j':
                    {
                        key = Path.GetFullPath(dir) + "\\A-4.wav";
                        p[26].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\A-4.png");
                        p[26].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'k':
                    {
                        key = Path.GetFullPath(dir) + "\\B-4.wav";
                        p[27].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\B-4.png");
                        p[27].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'l':
                    {
                        key = Path.GetFullPath(dir) + "\\C-5.wav";
                        p[28].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\C-5.png");
                        p[28].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'z':
                    {
                        key = Path.GetFullPath(dir) + "\\D-5.wav";
                        p[29].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\D-5.png");
                        p[29].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'x':
                    {
                        key = Path.GetFullPath(dir) + "\\E-5.wav";
                        p[30].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\E-5.png");
                        p[30].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'c':
                    {
                        key = Path.GetFullPath(dir) + "\\F-5.wav";
                        p[31].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\F-5.png");
                        p[31].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'v':
                    {
                        key = Path.GetFullPath(dir) + "\\G-5.wav";
                        p[32].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\G-5.png");
                        p[32].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'b':
                    {
                        key = Path.GetFullPath(dir) + "\\A-5.wav";
                        p[33].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\A-5.png");
                        p[33].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'n':
                    {
                        key = Path.GetFullPath(dir) + "\\B-5.wav";
                        p[34].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\B-5.png");
                        p[34].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
                case 'm':
                    {
                        key = Path.GetFullPath(dir) + "\\C-6.wav";
                        p[35].Open(new System.Uri(key));
                        pictureBox1.Image = Image.FromFile(@"Pictures\C-6.png");
                        p[35].Play();
                        noteLabel.Text = key.Substring(key.Length - 7, 3);
                    }
                    break;
            }

            tabLabel.Text = tabLabel.Text.Trim(' ');


        }

        
      

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            pictureBox1.Image = Image.FromFile(@"Pictures\PianoDefault.png");
        }

        private void button7_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {

            string key = "";

            System.Windows.Media.MediaPlayer[] p = new System.Windows.Media.MediaPlayer[36];
            for (int i = 0; i < 36; i++)
            {
                p[i] = new System.Windows.Media.MediaPlayer();
            }

            string dir = "Sounds_New";

            if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 1)))
            {
                key = Path.GetFullPath(dir) + "\\C-1.wav";
                p[0].Open(new System.Uri(key));
                p[0].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);

            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 2)))
            {
                key = Path.GetFullPath(dir) + "\\D-1.wav";
                p[1].Open(new System.Uri(key));
                p[1].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 3)))
            {
                key = Path.GetFullPath(dir) + "\\E-1.wav";
                p[2].Open(new System.Uri(key));
                p[2].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 4)))
            {
                key = Path.GetFullPath(dir) + "\\F-1.wav";
                p[3].Open(new System.Uri(key));
                p[3].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 5)))
            {
                key = Path.GetFullPath(dir) + "\\G-1.wav";
                p[4].Open(new System.Uri(key));
                p[4].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 6)))
            {
                key = Path.GetFullPath(dir) + "\\A-1.wav";
                p[5].Open(new System.Uri(key));
                p[5].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 7)))
            {
                key = Path.GetFullPath(dir) + "\\B-1.wav";
                p[6].Open(new System.Uri(key));
                p[6].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 8)))
            {
                key = Path.GetFullPath(dir) + "\\C-2.wav";
                p[7].Open(new System.Uri(key));
                p[7].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 9)))
            {
                key = Path.GetFullPath(dir) + "\\D-2.wav";
                p[8].Open(new System.Uri(key));
                p[8].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 10)))
            {
                key = Path.GetFullPath(dir) + "\\E-2.wav";
                p[9].Open(new System.Uri(key));
                p[9].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 11)))
            {
                key = Path.GetFullPath(dir) + "\\F-2.wav";
                p[10].Open(new System.Uri(key));
                p[10].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 12)))
            {
                key = Path.GetFullPath(dir) + "\\G-2.wav";
                p[11].Open(new System.Uri(key));
                p[11].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 13)))
            {
                key = Path.GetFullPath(dir) + "\\A-2.wav";
                p[12].Open(new System.Uri(key));
                p[12].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 14)))
            {
                key = Path.GetFullPath(dir) + "\\B-2.wav";
                p[13].Open(new System.Uri(key));
                p[13].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 15)))
            {
                key = Path.GetFullPath(dir) + "\\C-3.wav";
                p[14].Open(new System.Uri(key));
                p[14].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 16)))
            {
                key = Path.GetFullPath(dir) + "\\D-3.wav";
                p[15].Open(new System.Uri(key));
                p[15].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 17)))
            {
                key = Path.GetFullPath(dir) + "\\E-3.wav";
                p[16].Open(new System.Uri(key));
                p[16].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 18)))
            {
                key = Path.GetFullPath(dir) + "\\F-3.wav";
                p[17].Open(new System.Uri(key));
                p[17].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 19)))
            {
                key = Path.GetFullPath(dir) + "\\G-3.wav";
                p[18].Open(new System.Uri(key));
                p[18].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 20)))
            {
                key = Path.GetFullPath(dir) + "\\A-3.wav";
                p[19].Open(new System.Uri(key));
                p[19].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 21)))
            {
                key = Path.GetFullPath(dir) + "\\B-3.wav";
                p[20].Open(new System.Uri(key));
                p[20].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 22)))
            {
                key = Path.GetFullPath(dir) + "\\C-4.wav";
                p[21].Open(new System.Uri(key));
                p[21].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 23)))
            {
                key = Path.GetFullPath(dir) + "\\D-4.wav";
                p[22].Open(new System.Uri(key));
                p[22].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 24)))
            {
                key = Path.GetFullPath(dir) + "\\E-4.wav";
                p[23].Open(new System.Uri(key));
                p[23].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 25)))
            {
                key = Path.GetFullPath(dir) + "\\F-4.wav";
                p[24].Open(new System.Uri(key));
                p[24].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 26)))
            {
                key = Path.GetFullPath(dir) + "\\G-4.wav";
                p[25].Open(new System.Uri(key));
                p[25].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 27)))
            {
                key = Path.GetFullPath(dir) + "\\A-4.wav";
                p[26].Open(new System.Uri(key));
                p[26].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 28)))
            {
                key = Path.GetFullPath(dir) + "\\B-4.wav";
                p[27].Open(new System.Uri(key));
                p[27].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 29)))
            {
                key = Path.GetFullPath(dir) + "\\C-5.wav";
                p[28].Open(new System.Uri(key));
                p[28].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 30)))
            {
                key = Path.GetFullPath(dir) + "\\D-5.wav";
                p[29].Open(new System.Uri(key));
                p[29].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 31)))
            {
                key = Path.GetFullPath(dir) + "\\E-5.wav";
                p[30].Open(new System.Uri(key));
                p[30].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);

            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 32)))
            {
                key = Path.GetFullPath(dir) + "\\F-5.wav";
                p[31].Open(new System.Uri(key));
                p[31].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 33)))
            {
                key = Path.GetFullPath(dir) + "\\G-5.wav";
                p[32].Open(new System.Uri(key));
                p[32].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 34)))
            {
                key = Path.GetFullPath(dir) + "\\A-5.wav";
                p[33].Open(new System.Uri(key));
                p[33].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 35)))
            {
                key = Path.GetFullPath(dir) + "\\B-5.wav";
                p[34].Open(new System.Uri(key));
                p[34].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (43 + 15 * 36)))
            {
                key = Path.GetFullPath(dir) + "\\C-6.wav";
                p[35].Open(new System.Uri(key));
                p[35].Play();
                noteLabel.Text = key.Substring(key.Length - 7, 3);
            }
            else pictureBox1.Image = Image.FromFile(@"Pictures\PianoDefault.png");

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {

            if ((e.Y>95 && e.Y<183) && (e.X>32 && e.X<(32+15*1)))
                 pictureBox1.Image = Image.FromFile(@"Pictures_2\C-1.png");
            else if ((e.Y>95 && e.Y<183) && (e.X>32 && e.X<(32+15*2)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\D-1.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 3)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\E-1.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 4)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\F-1.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 5)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\G-1.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 6)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\A-1.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (32 + 15 * 7)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\B-1.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 8)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\C-2.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 9)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\D-2.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 10)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\E-2.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 11)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\F-2.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 12)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\G-2.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 13)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\A-2.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (35 + 15 * 14)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\B-2.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 15)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\C-3.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 16)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\D-3.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 17)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\E-3.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 18)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\F-3.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 19)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\G-3.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 20)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\A-3.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 21)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\B-3.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (37 + 15 * 22)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\C-4.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 23)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\D-4.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 24)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\E-4.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 25)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\F-4.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 26)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\G-4.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 27)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\A-4.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 28)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\B-4.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (39 + 15 * 29)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\C-5.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 30)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\D-5.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 31)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\E-5.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 32)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\F-5.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 33)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\G-5.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 34)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\A-5.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (41 + 15 * 35)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\B-5.png");
            else if ((e.Y > 95 && e.Y < 183) && (e.X > 32 && e.X < (43 + 15 * 36)))
                pictureBox1.Image = Image.FromFile(@"Pictures_2\C-6.png");
            else pictureBox1.Image = Image.FromFile(@"Pictures\PianoDefault.png");

        }

        private void tabButtton_Click(object sender, EventArgs e)
        {
            position = 0;
            string filename = @"Tabs\" + comboBox1.Items[comboBox1.SelectedIndex]+".txt";
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    tabLabel.Text = sr.ReadToEnd();
                    length = tabLabel.Text.Length;
                }
            }
            tabLabel.Text = tabLabel.Text.Replace('[',' ');
            tabLabel.Text = tabLabel.Text.Replace(']',' ');
            tabLabel.Text = tabLabel.Text.Trim(' ');
            tabLabel.Text = tabLabel.Text.Replace("  "," ");
            tabLabel.Text = tabLabel.Text + "  (end)";

        }

        private void dirButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog open = new FolderBrowserDialog();
            if (open.ShowDialog() != DialogResult.OK) return;
            dirName = open.SelectedPath;
            ShowTracks(dirName);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            button3_Click(sender, e);
        }

        private void помощьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            help = new Form2();
            help.Show();
        }

        private void tabRecord_Click(object sender, EventArgs e)
        {
            tabRecordIsPressed++;


            if (tabRecordIsPressed % 2 == 1)
            {
                tabRecord.Text = "Сохранить табулатуру";
                tabLabel.Text = "";
                tabLabel.ForeColor = Color.Red;
                tabButton.Enabled = false;
            }
           
            if (tabRecordIsPressed % 2 == 0)
            {
                Form3 name = new Form3();
                name.ShowDialog();
                MessageBox.Show("Файл "+ name.text+" успешно сохранен.");
                tabLabel.Text = "";
                tabRecord.Text = "Записать табулатуру";
                tabLabel.ForeColor = Color.Black;
                if (name.text == "" || name.text == null)
                    name.text = "Default";

                using (FileStream fs = new FileStream("Tabs\\" + name.text + ".txt", FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(tabs);
                    }
                }
                
                comboBox1.Items.Add(name.text);
                tabButton.Enabled = true;

            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (tabRecordIsPressed % 2 == 1)
            {
                if ((e.KeyCode == Keys.Back && tabLabel.Text.Length > 2))
                {
                    tabs = tabs.Substring(0, tabs.Length - 2);
                    tabLabel.Text = tabs.Substring(shift);
                }
            
                if ((e.KeyCode == Keys.Back && tabLabel.Text.Length == 1))
                {
                        tabs = tabs.Substring(0, tabs.Length - 2);
                        tabLabel.Text = tabs.Substring(shift);
                }
            }
            if (e.KeyCode == Keys.F1)
            {
                Form2 form2 = new Form2();
                form2.Show();
            }
            if (e.KeyCode == Keys.Escape)
            {
                DisposeWave();
                this.Dispose();
                this.Close();
            }
        }


    }
}
