private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                return;
            }
            //CheckDatabase();
            try
            {
                using (var connection = new SQLiteConnection("Data Source=" + Properties.Settings.Default.WaveformDatabase + ";Version=3"))
                    using (var command = new SQLiteCommand("SELECT fid FROM file WHERE location=@Location", connection))
                    {
                        command.Parameters.AddWithValue("Location", AudioFile);
                        int fid = -1;
                        connection.Open();

                        SQLiteDataReader r = command.ExecuteReader();
                        while (r.Read())
                        {
                            if (r["fid"] != null)
                            {
                                var res = r["fid"];
                                fid = (int)(Int64)res;
                            }
                        }
                        r.Close();

                        if (fid != -1)
                        {
                            ReadFromDatabase(command, fid);

                            // Update painters
                            WaveformPainterLeft.Invalidate();
                            WaveformPainterRight.Invalidate();
                        }
                        else
                        {
                            if (samples == null)
                            {
                                return;
                            }

                            InitializePainer(WaveformPainterLeft, lsamples);
                            InitializePainer(WaveformPainterRight, rsamples);
                        }
                        connection.Close();
                    }
            }
            catch (SQLiteException sQLiteException)
            {
                MessageBox.Show(String.Format("Grid_SizeChanged sQLiteException {0}", sQLiteException.Message), "Error Initializing database");
                return;
            }
            catch (Exception exception)
            {
                MessageBox.Show(String.Format("Grid_SizeChanged Exception {0}", exception.Message), "Error Initializing database");
                return;
            }
        }
        public void LoadAudio(string audioFile)
        {
            AudioFile = audioFile;
            //CheckDatabase();

            // Check if waveform is already exists in database
            using (var connection = new SQLiteConnection("Data Source=" + Properties.Settings.Default.WaveformDatabase + ";Version=3"))
                using (var command = new SQLiteCommand("SELECT fid FROM file WHERE location= @Location", connection))
                {
                    command.Parameters.AddWithValue("Location", AudioFile);
                    int fid = -1;
                    connection.Open();
                    try
                    {
                        //fid = (int)command.ExecuteScalar();
                        var r = command.ExecuteReader();
                        while (r.Read())
                        {
                            if (r["fid"] != null)
                            {
                                var res = r["fid"];
                                fid = (int)(Int64)res;
                            }
                        }
                        r.Close();


                        if (fid == -1)
                        {
                            // Read from file
                            ReadAudioFile();

                            // Update painters
                            InitializePainer(WaveformPainterLeft, lsamples);
                            InitializePainer(WaveformPainterRight, rsamples);

                            // Store to database
                            command.CommandText = "INSERT INTO file (location, subsong) VALUES (@Location,@Subsong )";
                            {
                                command.Parameters.AddWithValue("Location", AudioFile);
                                command.Parameters.AddWithValue("Subsong", "");
                                int lres = command.ExecuteNonQuery();
                                command.CommandText = @"select last_insert_rowid()";
                                var datar = command.ExecuteScalar();;
                                fid = (int)(Int64)datar;

                                List <float> allMinSampless = new List <float>();
                                List <float> allMaxSampless = new List <float>();
                                for (var i = 0; i < WaveformPainterLeft.NegativeSamples.Count; i++)
                                {
                                    allMinSampless.Add(WaveformPainterLeft.NegativeSamples[i]);
                                    allMinSampless.Add(WaveformPainterRight.NegativeSamples[i]);
                                }
                                for (var i = 0; i < WaveformPainterLeft.PositiveSamples.Count; i++)
                                {
                                    allMaxSampless.Add(WaveformPainterLeft.PositiveSamples[i]);
                                    allMaxSampless.Add(WaveformPainterRight.PositiveSamples[i]);
                                }

                                var dataMin = new byte[allMinSampless.Count * 4];
                                Buffer.BlockCopy(allMinSampless.ToArray(), 0, dataMin, 0, dataMin.Length);
                                var dataMax = new byte[allMaxSampless.Count * 4];
                                Buffer.BlockCopy(allMaxSampless.ToArray(), 0, dataMax, 0, dataMax.Length);

                                try
                                {
                                    command.CommandText = "INSERT INTO wave (fid,min,max,channels,compression)" +
                                                          "VALUES (@fid,@min,@max,@channels,@compression)";

                                    command.Parameters.Add("@fid", DbType.Int32).Value         = fid;
                                    command.Parameters.Add("@min", DbType.Binary).Value        = dataMin;
                                    command.Parameters.Add("@max", DbType.Binary).Value        = dataMax;
                                    command.Parameters.Add("@channels", DbType.Int32).Value    = 2;
                                    command.Parameters.Add("@compression", DbType.Int32).Value = 1;
                                    command.ExecuteNonQuery();
                                }
                                catch (SQLiteException sQLiteException)
                                {
                                    MessageBox.Show(String.Format("LoadAudio from file sQLiteException {0}", sQLiteException.Message), "Error reading from database");
                                }
                            }
                        }
                        else
                        {
                            ReadFromDatabase(command, fid);

                            // Update painters
                            WaveformPainterLeft.Invalidate();
                            WaveformPainterRight.Invalidate();
                        }
                        connection.Close();
                    }
                    catch (SQLiteException sQLiteException)
                    {
                        MessageBox.Show(String.Format("LoadAudio sQLiteException {0}", sQLiteException.Message), "Error Initializing database");
                    }
                }
        }