コード例 #1
0
        // event. click on 'Enregistrer audio..' in MenuStrip1
        private void enregistrerUnAudiowavToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form fen = new RecordSound();

            fen.ShowDialog();
        }
コード例 #2
0
        // event. click on 'Cacher' boutton
        private void cacherBtn_Click(object sender, EventArgs e)
        {
            // Désactivation du boutton 'Lire' du fichier audio de sortie + 'Extraction Rapide..'
            playOutputBtn.Enabled = ExtractionRapideBtn.Enabled = false;

            // remise à zéro de la barre de chargement
            progressBar1.Value = 0;

            // 1 - vérification du fichier audio d'entrée + la clé + le texte à cacher
            if (audioFileTxtBox.Text == "")
            {
                MessageBox.Show("Aucun fichier audio choisi !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (cleTxtBox.Text == "")
            {
                MessageBox.Show("Aucune clé choisie !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (txtAcacherTab1.Text == "")
            {
                MessageBox.Show("Aucun texte à cacher !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 2 - demande d'emplacement d'enregistrement du fichier audio de sortie
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Title      = "Enregistrer le fichier audio de sortie dans";
            saveFileDialog1.DefaultExt = "wav";
            saveFileDialog1.Filter     = "audio files (*.wav)|*.wav";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK) // si emplacement choisi
            {
                // Let's hide the message/text !

                // on commence le calcul du temp d'éxecution
                Stopwatch sw = new Stopwatch();
                sw.Start();

                // create streams
                Stream     sourceStream      = null;
                FileStream destinationStream = null;
                WaveStream audioStream       = null;

                // create a stream that contains the message, preceeded by its length
                Stream messageStream = ToolsAndFunctions.stringToStream(txtAcacherTab1.Text);
                // create a stream that contains the key
                Stream keyStream = ToolsAndFunctions.stringToStream(cleTxtBox.Text);

                long countSamplesRequired = 0; // var to count samples required (we use it in catch() so this is the best place)

                try
                {
                    //how man samples do we need?
                    countSamplesRequired = WaveUtility.CheckKeyForMessage(keyStream, messageStream.Length);

                    if (countSamplesRequired > Int32.MaxValue)
                    {
                        throw new Exception("Message too long, or bad key! This message/key combination requires " + countSamplesRequired + " samples, only " + Int32.MaxValue + " samples are allowed.");
                    }

                    // open source stream
                    sourceStream = new FileStream(audioFileTxtBox.Text, FileMode.Open);

                    // create destination stream
                    destinationStream = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write);

                    //copy the carrier file's header
                    audioStream = new WaveStream(sourceStream, destinationStream);
                    if (audioStream.Length <= 0)
                    {
                        throw new Exception("Invalid WAV file");
                    }

                    //are there enough samples in the carrier wave?
                    if (countSamplesRequired > audioStream.CountSamples)
                    {
                        String errorReport = "The carrier file is too small for this message and key!\r\n"
                                             + "Samples available: " + audioStream.CountSamples + "\r\n"
                                             + "Samples needed: " + countSamplesRequired + "\r\n\r\n"
                                             + "\tOuvrir la fenêtre d'enregistrement audio ?";
                        throw new Exception(errorReport);
                    }

                    //hide the message
                    this.Cursor = Cursors.WaitCursor;
                    WaveUtility utility = new WaveUtility(audioStream, destinationStream);
                    utility.Hide(messageStream, keyStream);

                    // set output audio file path
                    outputAudioTxtBox.Text = saveFileDialog1.FileName;

                    // on arrête le calcul du temp d'éxecution + on l'affiche
                    sw.Stop();
                    execTime.Text = sw.ElapsedMilliseconds.ToString() + " ms";

                    // activate 'Lire' boutton + 'Extraction Rapide..'
                    playOutputBtn.Enabled = ExtractionRapideBtn.Enabled = true;

                    // chargement complet de la barre de chargement
                    progressBar1.Value = 100;
                }
                catch (Exception ex)
                {
                    //this.Cursor = Cursors.Default;
                    // if Message contains "Samples needed" , mean that wav file is not enought
                    if (ex.Message.Contains("Samples needed"))
                    {
                        if (MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) == System.Windows.Forms.DialogResult.Yes)
                        {
                            Form fen = new RecordSound(countSamplesRequired);
                            fen.ShowDialog();
                        }
                    }
                    else
                    {
                        MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                finally
                {
                    if (keyStream != null)
                    {
                        keyStream.Close();
                    }
                    if (messageStream != null)
                    {
                        messageStream.Close();
                    }
                    if (audioStream != null)
                    {
                        audioStream.Close();
                    }
                    if (sourceStream != null)
                    {
                        sourceStream.Close();
                    }
                    if (destinationStream != null)
                    {
                        destinationStream.Close();
                    }
                    this.Cursor = Cursors.Default;
                    if (sw.IsRunning)
                    {
                        sw.Stop();
                    }
                } // fin try
            }     // fin if
        }