private void exportBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < _sndAliasBank.Entries.Count; i++)
            {
                if (exportBackgroundWorker.CancellationPending)
                {
                    return;
                }

                var    entry = _sndAliasBank.Entries[i];
                string relativePath;
                if (!_useOriginalTreeStructure || !SndAliasNameDatabase.Names.TryGetValue(entry.Identifier, out relativePath))
                {
                    relativePath = "Sound " + (i + 1) + SndAliasBankHelper.GetExtensionFromFormat(entry.Format);
                }
                else
                {
                    relativePath += SndAliasBankHelper.GetExtensionFromFormat(entry.Format);
                }

                using (var audioStream = SndAliasBankHelper.GetAudioStreamFromEntry(entry))
                {
                    try
                    {
                        var    fullPath = Path.Combine(_outputDirectory, relativePath);
                        string directoryPath;
                        if (relativePath.IndexOf('\\') != -1 && !Directory.Exists((directoryPath = Path.GetDirectoryName(fullPath))))
                        {
                            Directory.CreateDirectory(directoryPath);
                        }
                        using (var fs = new FileStream(fullPath, FileMode.Create,
                                                       FileAccess.Write, FileShare.Read))
                            audioStream.CopyTo(fs);
                        exportBackgroundWorker.ReportProgress((i + 1) * 100 / _sndAliasBank.Entries.Count, Path.GetFileName(fullPath));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Black Ops II Sound Studio",
                                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }
            }

            MessageBox.Show("All audio entries have been exported successfully.",
                            "Black Ops II Sound Studio", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void exportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var selectedRow = audioEntriesDataGridView.SelectedRows[0];
            var name        = selectedRow.Cells[0].Value.ToString();
            var entry       = (SndAssetBankEntry)selectedRow.Tag;

            using (var saveFileDialog = new SaveFileDialog())
            {
                var extension = SndAliasBankHelper.GetExtensionFromFormat(entry.Format);
                if (string.IsNullOrEmpty(extension))
                {
                    saveFileDialog.Filter = "All Files|*.*";
                }
                else
                {
                    saveFileDialog.Filter = extension.Substring(1).ToUpperInvariant() + " Files|*" + extension;
                }

                if (!name.EndsWith(extension))
                {
                    name += extension;
                }
                saveFileDialog.FileName = name;

                if (saveFileDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                try
                {
                    using (var audioStream = SndAliasBankHelper.GetAudioStreamFromEntry(entry))
                        using (var fs = new FileStream(saveFileDialog.FileName, FileMode.Create,
                                                       FileAccess.Write, FileShare.Read))
                            audioStream.CopyTo(fs);

                    MessageBox.Show(Path.GetFileName(saveFileDialog.FileName) + " has been exported successfully.",
                                    "Black Ops II Sound Studio", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Black Ops II Sound Studio",
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
        private void playToolStripButton_Click(object sender, EventArgs e)
        {
            // Resume the song if the song was paused.
            if (_player != null && _lastSelectedRow == audioEntriesDataGridView.SelectedRows[0])
            {
                if (_player.PlaybackState == PlaybackState.Paused)
                {
                    _player.Play();
                }
                return;
            }

            // Stop the current song and free resources.
            stopToolStripButton.PerformClick();
            audioEntriesDataGridView.Enabled = false;
            playToolStripButton.Enabled      = false;
            mainMenuStrip.Enabled            = false;

            // Find the current selected entry.
            var entry = (SndAssetBankEntry)(_lastSelectedRow = audioEntriesDataGridView.SelectedRows[0]).Tag;

            ThreadPool.QueueUserWorkItem(x =>
            {
                // Begin decoding or create a compatible IWaveProvider interface.
                IWaveProvider provider = null;

                if (entry.Format == AudioFormat.FLAC)
                {
                    _audioStream = DecodeFLAC(entry);

                    if (_audioStream != null)
                    {
                        _wavFileReader = new WaveFileReader(_audioStream);
                        provider       = _wavFileReader;
                    }
                    else
                    {
                        MessageBox.Show("Something happened while trying to decode/prepare the audio.",
                                        "Black Ops 2 Sound Studio", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        _currentPlayIndex = -1;
                        Invoke(new MethodInvoker(() =>
                        {
                            currentTimetoolStripLabel.Text   = "";
                            playToolStripButton.Enabled      = true;
                            audioEntriesDataGridView.Enabled = true;
                            mainMenuStrip.Enabled            = true;
                        }));
                        return;
                    }
                }
                else if (entry.Format == AudioFormat.PCMS16)
                {
                    _audioStream   = SndAliasBankHelper.DecodeHeaderlessWav(entry);
                    _wavFileReader = new WaveFileReader(_audioStream);
                    provider       = _wavFileReader;
                }
                else if (entry.Format == AudioFormat.MP3)
                {
                    _audioStream   = new MemoryStream(entry.Data.Get());
                    _mp3FileReader = new Mp3FileReader(_audioStream);
                    provider       = _mp3FileReader;
                }
                else if (entry.Format == AudioFormat.XMA4)
                {
                    Invoke(new MethodInvoker(() => currentTimetoolStripLabel.Text = "Please wait, decoding audio..."));

                    using (var xmaStream = SndAliasBankHelper.AddXMAHeader(entry))
                        _audioStream = ConvertHelper.ConvertXMAToWAV(xmaStream);

                    if (_audioStream != null)
                    {
                        _wavFileReader = new WaveFileReader(_audioStream);
                        provider       = _wavFileReader;
                    }
                }

                // Begin playing the audio on the UI thread.
                Invoke(new MethodInvoker(() =>
                {
                    currentTimetoolStripLabel.Text   = "";
                    playToolStripButton.Enabled      = true;
                    audioEntriesDataGridView.Enabled = true;
                    mainMenuStrip.Enabled            = true;

                    // Ensure a valid provided was provided.
                    if (provider != null)
                    {
                        pauseToolStripButton.Enabled = true;
                        stopToolStripButton.Enabled  = true;

                        _player = new WaveOut();
                        _player.Init(provider);
                        playerTimeTimer.Start();
                        _player.PlaybackStopped += _player_PlaybackStopped;
                        _player.Play();
                    }
                    else
                    {
                        MessageBox.Show("Playback for the selected format is not supported or audio decoding failed.",
                                        "Black Ops 2 Sound Studio", MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                    }
                }));
            });
        }
        private void UpdateAudioEntries()
        {
            audioEntriesDataGridView.Rows.Clear();

            // Set autosize modes.
            for (int i = 0; i < audioEntriesDataGridView.Columns.Count - 1; i++)
            {
                if (audioEntriesDataGridView.Columns[i].HeaderText != "Sample Rate")
                {
                    audioEntriesDataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                }
                else
                {
                    audioEntriesDataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                }
            }

            // Add the entries.
            DataGridViewRow[] rows = new DataGridViewRow[_sndAliasBank.Entries.Count];
            for (int i = 0; i < _sndAliasBank.Entries.Count; i++)
            {
                var    entry = _sndAliasBank.Entries[i];
                string name;
                if (SndAliasNameDatabase.Names.TryGetValue(entry.Identifier, out name))
                {
                    name = showFullNameToolStripMenuItem.Checked ? name : Path.GetFileName(name);
                }
                else
                {
                    name = "Sound #" + (i + 1) + SndAliasBankHelper.GetExtensionFromFormat(entry.Format);
                }
                var row = new DataGridViewRow();
                row.CreateCells(audioEntriesDataGridView,
                                name,
                                entry.Offset,
                                entry.Size,
                                SndAliasBankHelper.GetFormatName(entry.Format),
                                entry.Loop,
                                entry.ChannelCount,
                                FormatSampleRate(entry.SampleRate),
                                BitConverter.ToString(_sndAliasBank.Checksums[i]).Replace("-", ""));
                row.ContextMenuStrip = audioEntryContextMenuStrip;
                row.Tag = entry;
                rows[i] = row;
            }

            audioEntriesDataGridView.Rows.AddRange(rows);

            // Fix to allow user to manually size.
            for (int i = 0; i < audioEntriesDataGridView.Columns.Count - 1; i++)
            {
                var column = audioEntriesDataGridView.Columns[i];
                if (column.AutoSizeMode != DataGridViewAutoSizeColumnMode.AllCells)
                {
                    continue;
                }
                int width = column.GetPreferredWidth(column.AutoSizeMode, true);
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
                column.Width        = width;
            }
        }