コード例 #1
0
        private void buttonDeviceSelect_Click(object sender, RoutedEventArgs e)
        {
            int latencyMillisec = -1;

            try {
                latencyMillisec = Int32.Parse(textBoxLatency.Text);
            } catch (Exception ex) {
                textBoxLog.Text += string.Format("{0}\r\n", ex);
            }
            if (latencyMillisec <= 0)
            {
                latencyMillisec     = DEFAULT_OUTPUT_LATENCY_MS;
                textBoxLatency.Text = string.Format("{0}", DEFAULT_OUTPUT_LATENCY_MS);
            }

            try {
                var bytes = Int32.Parse(textBoxRecMaxMB.Text) * 1024L * 1024L;
                if (0x7fffffff < bytes)
                {
                    string s = string.Format("E: 録音バッファーサイズを2047MB以下に減らして下さい。\r\n");
                    textBoxLog.Text += s;
                    MessageBox.Show(s);
                    return;
                }
                m_bufferBytes = (int)bytes;
            } catch (Exception ex) {
                textBoxLog.Text += string.Format("{0}\r\n", ex);
            }
            if (m_bufferBytes < 0)
            {
                m_bufferBytes        = DEFAULT_BUFFER_SIZE_MB * 1024 * 1024;
                textBoxRecMaxMB.Text = string.Format("{0}", DEFAULT_BUFFER_SIZE_MB);
            }
            try {
                mCapturedPcmData = null;
                mCapturedPcmData = new byte[m_bufferBytes];
            } catch (Exception ex) {
                textBoxLog.Text += string.Format("{0}\r\n", ex);
                MessageBox.Show(string.Format("E: おそらくメモリ不足ですので、録音バッファーサイズを減らして下さい。\r\n{0}", ex));
                return;
            }

            int hr = wasapi.ChooseDevice(listBoxDevices.SelectedIndex);

            textBoxLog.Text += string.Format("wasapi.ChooseDevice({0}) {1:X8}\r\n",
                                             listBoxDevices.SelectedItem.ToString(), hr);
            if (hr < 0)
            {
                return;
            }

            WasapiCS.DataFeedMode dfm = WasapiCS.DataFeedMode.EventDriven;
            if (true == radioButtonTimerDriven.IsChecked)
            {
                dfm = WasapiCS.DataFeedMode.TimerDriven;
            }

            wasapi.SetDataFeedMode(dfm);
            wasapi.SetLatencyMillisec(latencyMillisec);
            hr = wasapi.Setup(mSamplingFrequency, mSampleFormat, mNumChannels);
            textBoxLog.Text += string.Format("wasapi.Setup({0}, {1}, {2}, {3}) {4:X8}\r\n",
                                             mSamplingFrequency, mSampleFormat, latencyMillisec, dfm, hr);
            if (hr < 0)
            {
                wasapi.Unsetup();
                textBoxLog.Text += string.Format("wasapi.Unsetup()\r\n");
                CreateDeviceList();
                string sDfm = DfmToStr(dfm);
                string s    = string.Format("E: wasapi.Setup({0}, {1}, {2}, {3})失敗。{4:X8}\nこのプログラムのバグか、オーディオデバイスが{0}Hz {1} レイテンシー{2}ms {3}に対応していないのか、どちらかです。\r\n",
                                            mSamplingFrequency, mSampleFormat,
                                            latencyMillisec, sDfm, hr);
                textBoxLog.Text += s;
                MessageBox.Show(s);
                return;
            }

            buttonDeviceSelect.IsEnabled     = false;
            buttonDeselect.IsEnabled         = true;
            buttonRec.IsEnabled              = true;
            buttonInspectDevice.IsEnabled    = false;
            groupBoxWasapiSettings.IsEnabled = false;
        }