Example #1
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            SignalGeneraterWorkerArg sgwa = (SignalGeneraterWorkerArg)e.Argument;
            SignalGenerator          sg   = new SignalGenerator();

            backgroundWorker1.ReportProgress(1, "出力データの準備開始。\r\n");

            WavData wavData;
            SignalGeneratorResult cwdr
                = sg.GenerateSignal(sgwa.sgParams, out wavData);

            switch (cwdr)
            {
            case SignalGeneratorResult.Success:
                backgroundWorker1.ReportProgress(2, "出力データの準備完了。\r\n");
                break;

            case SignalGeneratorResult.LevelOver:
                backgroundWorker1.ReportProgress(2, "レベルオーバー。出力レベルを下げて下さい。\r\n");
                break;

            default:
                System.Diagnostics.Debug.Assert(false);
                break;
            }

            switch (sgwa.outputMode)
            {
            case OutputMode.WavFile:
                if (!WriteWavFile(wavData, sgwa.outputPath))
                {
                    backgroundWorker1.ReportProgress(3, string.Format("書き込み失敗: {0}\r\n", sgwa.outputPath));
                    return;
                }
                backgroundWorker1.ReportProgress(3, string.Format("書き込み成功: {0}\r\n", sgwa.outputPath));
                break;

            case OutputMode.Asio:
            {
                // short → int に拡張
                int[] asioData = new int[wavData.NumSamples];
                for (int i = 0; i < wavData.NumSamples; ++i)
                {
                    asioData[i] = wavData.Sample16Get(0, i) << 16;
                }
                // 出力デバイスに出力データをセット
                foreach (int ch in sgwa.outputChannels)
                {
                    asio.OutputSet(ch, asioData, true);
                }
                // 開始
                asio.Start();

                /* backgroundWorker1.ReportProgress(3,
                 *  string.Format("出力開始。{0}Hz {1} {2}dB\r\n", sgwa.sgParams.freq, sgwa.sgParams.ss, sgwa.sgParams.dB)); */
                while (!asio.Run())
                {
                }
                backgroundWorker1.ReportProgress(3, "出力終了。\r\n");
            }
            break;

            default:
                System.Diagnostics.Debug.Assert(false);
                break;
            }
        }
Example #2
0
        private void DoWork(object o, DoWorkEventArgs args)
        {
            Console.WriteLine("DoWork started\n");

            int count = 0;

            while (!asio.Run())
            {
                ++count;
                Console.WriteLine("\nForm1.DoWork() count={0} m_seconds={1}", count, m_seconds);
                int percent = 100 * count / m_seconds;
                if (100 < percent)
                {
                    percent = 100;
                }
            }
            int[] recordedData     = asio.RecordedDataGet(m_inputChannelNum, m_seconds * SAMPLE_RATE);
            PcmSamples1Channel ch0 = new PcmSamples1Channel(m_seconds * SAMPLE_RATE, 16);
            int max = 0;
            int min = 0;

            for (int i = 0; i < recordedData.Length; ++i)
            {
                if (max < recordedData[i])
                {
                    max = recordedData[i];
                }
                if (recordedData[i] < min)
                {
                    min = recordedData[i];
                }
            }
            Console.WriteLine("max={0} min={1}", max, min);

            if (max < -min)
            {
                max = -min;
            }
            double mag = 32767.0 / max;

            Console.WriteLine("mag={0}", mag);

            for (int i = 0; i < recordedData.Length; ++i)
            {
                ch0.Set16(i, (short)(recordedData[i] * mag));
            }

            List <PcmSamples1Channel> chList = new List <PcmSamples1Channel>();

            chList.Add(ch0);

            WavData wd = new WavData();

            wd.Create(SAMPLE_RATE, 16, chList);
            using (BinaryWriter bw = new BinaryWriter(File.Open(m_writeFilePath, FileMode.Create))) {
                wd.Write(bw);
            }

            args.Result = 0;
            Console.WriteLine("DoWork end\n");
        }