コード例 #1
0
        ////////////////////////////////////////////////////////

        public SignalGeneratorResult GenerateSignal(SignalGenerateParams s, out WavData wavData)
        {
            List <PcmSamples1Channel> samples = new List <PcmSamples1Channel>();

            int nSample           = s.seconds * s.sampleRate;
            PcmSamples1Channel ch = new PcmSamples1Channel(nSample, s.bitsPerSample);

            samples.Add(ch);

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            SignalGeneratorResult result = SignalGeneratorResult.Success;
            double truncFreq             = (s.sampleRate / 2) * s.truncationRatio;

            switch (s.ss)
            {
            case SignalShape.SineWave:
                result = GenerateSineWave(ch, s.sampleRate, s.freq, s.amplitude);
                break;

            case SignalShape.SquareWave:
                result = GenerateSquareWave(ch, s.sampleRate, s.freq, s.amplitude, truncFreq);
                break;

            case SignalShape.SawToothWaveDesc:
                result = GenerateSawToothWave(ch, s.sampleRate, s.freq, s.amplitude, truncFreq, false);
                break;

            case SignalShape.SawToothWaveAsc:
                result = GenerateSawToothWave(ch, s.sampleRate, s.freq, s.amplitude, truncFreq, true);
                break;

            case SignalShape.TriangleWave:
                result = GenerateTriangleWave(ch, s.sampleRate, s.freq, s.amplitude, truncFreq);
                break;

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

            sw.Stop();
            Console.WriteLine("{0} ms", sw.ElapsedMilliseconds);


            wavData = new WavData();
            wavData.Create(s.sampleRate, s.bitsPerSample, samples);
            return(result);
        }
コード例 #2
0
        private bool SaveWav(int sampleRate, int bitsPerSample, List <PcmSamples1Channel> samples, string path)
        {
            bool ret = true;

            WavData wav = new WavData();

            wav.Create(sampleRate, bitsPerSample, samples);

            try {
                using (BinaryWriter bw = new BinaryWriter(File.Open(path, FileMode.CreateNew))) {
                    wav.Write(bw);
                }
            } catch (Exception ex) {
                resultString = ex.ToString();
                ret          = false;
            }
            return(ret);
        }
コード例 #3
0
        static void Main(string[] args)
        {
            OptionInfo optionInfo = new OptionInfo(2);

            ParseOptions(ref optionInfo, args);

            WavData ch0Wav = ReadWavFromFile(optionInfo.channels[0].chFileName);
            WavData ch1Wav = ReadWavFromFile(optionInfo.channels[1].chFileName);

            if (ch0Wav.NumSamples != ch1Wav.NumSamples)
            {
                Console.WriteLine("E: NumSamples mismatch. ch0.numSamples={0}, ch1.numSamples={1}",
                                  ch0Wav.NumSamples, ch1Wav.NumSamples);
                PrintUsageAndExit();
            }

            WavData writeWav = new WavData();
            List <PcmSamples1Channel> channels = new List <PcmSamples1Channel>();

            for (int i = 0; i < 2; ++i)
            {
                channels.Add(new PcmSamples1Channel(ch0Wav.NumSamples, ch0Wav.BitsPerSample));
            }

            int ch0UseCh = optionInfo.channels[0].chFileUseCh;
            int ch1UseCh = optionInfo.channels[1].chFileUseCh;

            for (int i = 0; i < ch0Wav.NumSamples; ++i)
            {
                channels[0].Set16(i, ch0Wav.Sample16Get(ch0UseCh, i));
                channels[1].Set16(i, ch1Wav.Sample16Get(ch1UseCh, i));
            }

            writeWav.Create(ch0Wav.SampleRate, ch0Wav.BitsPerSample, channels);

            try {
                using (BinaryWriter bw = new BinaryWriter(File.Open(optionInfo.outputWavFileName, FileMode.CreateNew))) {
                    writeWav.Write(bw);
                }
            } catch (Exception ex) {
                Console.WriteLine("E: {0}", ex);
                PrintUsageAndExit();
            }
        }
コード例 #4
0
ファイル: Form1.cs プロジェクト: kekyo/PlayPcmWin
        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");
        }
コード例 #5
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");
        }
コード例 #6
0
        ////////////////////////////////////////////////////////
        public SignalGeneratorResult GenerateSignal(SignalGenerateParams s, out WavData wavData)
        {
            List<PcmSamples1Channel> samples = new List<PcmSamples1Channel>();

            int nSample = s.seconds * s.sampleRate;
            PcmSamples1Channel ch = new PcmSamples1Channel(nSample, s.bitsPerSample);
            samples.Add(ch);

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            SignalGeneratorResult result = SignalGeneratorResult.Success;
            double truncFreq = (s.sampleRate / 2) * s.truncationRatio;

            switch (s.ss) {
            case SignalShape.SineWave:
                result = GenerateSineWave(ch, s.sampleRate, s.freq, s.amplitude);
                break;
            case SignalShape.SquareWave:
                result = GenerateSquareWave(ch, s.sampleRate, s.freq, s.amplitude, truncFreq);
                break;
            case SignalShape.SawToothWaveDesc:
                result = GenerateSawToothWave(ch, s.sampleRate, s.freq, s.amplitude, truncFreq, false);
                break;
            case SignalShape.SawToothWaveAsc:
                result = GenerateSawToothWave(ch, s.sampleRate, s.freq, s.amplitude, truncFreq, true);
                break;
            case SignalShape.TriangleWave:
                result = GenerateTriangleWave(ch, s.sampleRate, s.freq, s.amplitude, truncFreq);
                break;
            default:
                System.Diagnostics.Debug.Assert(false);
                break;
            }

            sw.Stop();
            Console.WriteLine("{0} ms", sw.ElapsedMilliseconds);

            wavData = new WavData();
            wavData.Create(s.sampleRate, s.bitsPerSample, samples);
            return result;
        }