public void AnalyzeSignal_KnownSignal_ReturnsExpectedValues()
        {
            var audioStream = new MemoryStream();

            var audioGenerator    = new AudioGenerator(audioStream);
            var fskAudioGenerator = new FskAudioGenerator(audioGenerator);

            fskAudioGenerator.GenerateAudio(_binaryFskAnalyzerSettings.BaudRate,
                                            _binaryFskAnalyzerSettings.SpaceFrequency, _binaryFskAnalyzerSettings.MarkFrequency,
                                            _bitManipulator.StringToBits(Resources.LoremIpsumTestString));

            var audioAnalyzer     = new AudioAnalyzer(audioStream, audioGenerator);
            var binaryFskAnalyzer = (IBinaryFskAnalyzer) new BinaryFskAnalyzer(audioAnalyzer, new ZeroCrossingsFrequencyDetector(), _binaryFskAnalyzerSettings);
            var results           = binaryFskAnalyzer.AnalyzeSignal();

            var result = binaryFskAnalyzer.AnalyzeSignal();
            var bits   = new List <bool>();

            foreach (var frame in result.AnalysisFrames)
            {
                if (frame.Bit.HasValue == true)
                {
                    bits.Add(frame.Bit.Value);
                }
            }

            var ascii = BitManipulator.BitsToString(bits);

            Assert.AreEqual(Resources.LoremIpsumTestString, ascii);
        }
Esempio n. 2
0
        /// <summary>This function will generate a sound from a function you provide! The
        /// function is called once for each sample in the duration. As an example, it
        /// may be called 48,000 times for each second of duration.</summary>
        /// <param name="generator">This function takes a time value as an argument, which
        /// will range from 0-duration, and should return a value from -1 - +1 representing
        /// the audio wave at that point in time.</param>
        /// <param name="duration">In seconds, how long should the sound be?</param>
        /// <returns>Returns a generated sound effect! Or null if something went wrong.</returns>
        public static Sound Generate(AudioGenerator generator, float duration)
        {
            AudioGenerator tmpGen = generator;
            IntPtr         inst   = NativeAPI.sound_generate(tmpGen, duration);

            return(inst == IntPtr.Zero ? null : new Sound(inst));
        }
        private void SendAudio()
        {
            Logger.Info("Sending Audio... Please Wait");
            AudioGenerator mp3       = new AudioGenerator(mp3Path, volume, SpeakerGender, SpeakerCulture);
            var            opusBytes = mp3.GetOpusBytes();
            int            count     = 0;

            CancellationTokenSource tokenSource = new CancellationTokenSource();

            //get all the audio as Opus frames of 40 ms
            //send on 40 ms timer

            //when empty - disconnect
            //user timer for accurate sending
            var _timer = new Timer(() =>
            {
                if (!finished.IsCancellationRequested)
                {
                    if (count < opusBytes.Count)
                    {
                        udpVoiceHandler.Send(opusBytes[count], opusBytes[count].Length, freq, modulationBytes);
                        count++;

                        if (count % 50 == 0)
                        {
                            Logger.Info($"Playing audio - sent {count * 40}ms - {((float)count / (float)opusBytes.Count) * 100.0:F0}% ");
                        }
                    }
                    else
                    {
                        tokenSource.Cancel();
                    }
                }
                else
                {
                    Logger.Error("Client Disconnected");
                    tokenSource.Cancel();
                    return;
                }
            }, TimeSpan.FromMilliseconds(40));

            _timer.Start();

            //wait for cancel
            tokenSource.Token.WaitHandle.WaitOne();
            _timer.Stop();

            Logger.Info("Finished Sending Audio");
            finished.Cancel();
        }
Esempio n. 4
0
        public void Start()
        {
            double sampleRate      = (double)numericUpDown_sampleRate.Value;
            double amplitude       = (double)numericUpDown_amplitude.Value;
            double targetFrequency = (double)numericUpDown_targetFreq.Value;
            double duration        = (double)numericUpDown_Duration.Value / 1000;

            AITask.AITask input  = _globalInfo.AITask;
            AOTask.AOTask output = _globalInfo.AOTask;

            output.SetUpdateRate(sampleRate);
            input.SetSampleRate(sampleRate);
            double channelRange = (double)(numericUpDown_amplitude.Value);

            output.AddChannel(comboBox_aoChannel.SelectedIndex, -1 * channelRange, channelRange);
            input.AddChannel(comboBox_testChannel.SelectedIndex, -1 * channelRange, channelRange);

            double[] outData = AudioGenerator.SingleToneWaveform(output.GetUpdateRate(),
                                                                 (uint)(output.GetUpdateRate() * duration), amplitude, targetFrequency, 0);
            output.SetSamplesToUpdate(outData.Length);
            double outTime = outData.Length / output.GetUpdateRate();

            input.SetSamplesToAcquire((int)(outTime * input.GetSampleRate() * GlobalInfo.ExtraReadTime));
            output.WriteData(outData, -1);

            input.Start();
            output.Start();

            _testData = new double[input.GetSamplesToAcquire()];
            input.ReadData(ref _testData, _testData.Length, GlobalInfo.ReadTimeOut);
            //Added by Way
            output.Stop();
            input.Stop();
//            easyChart_data.Plot(_testData);
            _globalInfo.Mainform.Invoke(new Action <double[], double, double>(easyChart_data.Plot), _testData, 0, 1);

            double[] spectrum = new double[_testData.Length / 2];
            double   df;

            //Change unit to dBm
            Spectrum.PowerSpectrum(_testData, input.GetSampleRate(), ref spectrum, out df, SpectrumUnits.V2);
            _globalInfo.Mainform.Invoke(new Action <double[], double, double>(easyChart_spectrum.Plot), spectrum, 0, df);

//            double amp, thd, nr, thdPlusNRatio;
//            double[] harmonicPower;
//            SeeSharpTools.JY.Audio.AudioAnalyzer.AnalyzeSingleToneWave(readWaveform,
//                targetFrequency, sampleRate, out amp, out thd, out nr, out thdPlusNRatio,
//                out harmonicPower);
            _analyzeSize = (uint)Math.Ceiling(outTime * input.GetSampleRate());
        }
Esempio n. 5
0
        public double[] Generate()
        {
            double amplitude = (double)numericUpDown_amplitude.Value;
            double frequency = (double)numericUpDown_freq.Value;
            double duration  = (double)numericUpDown_duration.Value / 1000;

            GlobalInfo globalInfo = GlobalInfo.GetInstance();

            double updateRate = globalInfo.AOTask.GetUpdateRate();

            double[] outData = AudioGenerator.SingleToneWaveform(updateRate, (uint)(updateRate * duration),
                                                                 amplitude, frequency, 0);
            return(outData);
        }
Esempio n. 6
0
        public double[] Generate()
        {
            double amplitude    = (double)numericUpDown_amplitude1.Value;
            double frequencyMin = (double)numericUpDown_freq1.Value;
            ushort points       = (ushort)numericUpDown_points.Value;
            double frequencyMax = (double)numericUpDown_freq2.Value;
            double duration     = (double)numericUpDown_duration.Value / 1000;

            GlobalInfo globalInfo = GlobalInfo.GetInstance();

            double updateRate = globalInfo.AOTask.GetUpdateRate();

            double[] outData = AudioGenerator.MultiToneWaveform(updateRate, (uint)(updateRate * duration), amplitude, frequencyMin, frequencyMax, points);
            return(outData);
        }
Esempio n. 7
0
        public PlayingList(PlayList.PlayList PlayList)
        {
            this.NeedToStopAudio   = false;
            this.NeedToStartAudio  = false;
            this.NeedToDeleteAudio = false;
            this.AudioIndex        = 0;
            this.Title             = PlayList.PlayListInfo.Title;
            this.Audios            = new List <Audio.Audio>(PlayList.Audios.Select(x => x.Value).ToArray());


            this.SoundOut          = AudioGenerator.GetSoundOut();
            this.SoundOut.Stopped += SoundOut_Stopped;
            ControllingThread      = new Thread(ControllingThreadEntryPoint);
            ControllingThread.Start();
            Start();
        }
Esempio n. 8
0
        public double[] Generate()
        {
            double amplitude1 = (double)numericUpDown_amplitude1.Value;
            double frequency1 = (double)numericUpDown_freq1.Value;
            double amplitude2 = (double)numericUpDown_amplitude2.Value;
            double frequency2 = (double)numericUpDown_freq2.Value;
            double duration   = (double)numericUpDown_duration.Value / 1000;

            GlobalInfo globalInfo = GlobalInfo.GetInstance();

            double updateRate = globalInfo.AOTask.GetUpdateRate();

            double[] outData = AudioGenerator.DualToneWaveform(updateRate, frequency1, frequency2, amplitude1,
                                                               amplitude2, (uint)(updateRate * duration));
            return(outData);
        }
Esempio n. 9
0
        public static void Main(string[] args)
        {
            var bitManipulator = new BitManipulator();
            var myBits         = bitManipulator.StringToBits(Resources.BigLebowskiQuote);

            Console.WriteLine($"Length of string in bits: {myBits.Count}");

            var audioStream = new MemoryStream();

            var binaryFskAnalyzerSettings = new Bell103BinaryFskAnalyzerSettings();

            var audioGenerator    = new AudioGenerator(audioStream);
            var fskAudioGenerator = new FskAudioGenerator(audioGenerator);

            fskAudioGenerator.GenerateAudio(binaryFskAnalyzerSettings.BaudRate,
                                            binaryFskAnalyzerSettings.SpaceFrequency, binaryFskAnalyzerSettings.MarkFrequency, myBits);

            var audioLengthInMicroseconds = (int)(myBits.Count * Math.Pow(10, 6) / binaryFskAnalyzerSettings.BaudRate);

            Console.WriteLine($"Length of audio in seconds: {audioLengthInMicroseconds / Math.Pow(10, 6):N1}");
            Console.WriteLine();

            Console.WriteLine(Resources.BigLebowskiQuote);
            Console.WriteLine();

            AudioAnalyzer.Play(audioStream, audioLengthInMicroseconds / 1000);

            audioGenerator    = new AudioGenerator(audioStream);
            fskAudioGenerator = new FskAudioGenerator(audioGenerator);
            fskAudioGenerator.GenerateAudio(binaryFskAnalyzerSettings.BaudRate,
                                            binaryFskAnalyzerSettings.SpaceFrequency, binaryFskAnalyzerSettings.MarkFrequency, myBits);

            var binaryFskAnalyzer = new BinaryFskAnalyzer(new AudioAnalyzer(audioStream, audioGenerator), new ZeroCrossingsFrequencyDetector(), binaryFskAnalyzerSettings);

            Console.WriteLine($"Window position start {binaryFskAnalyzerSettings.WindowPositionStartMicroseconds:N3} us, window position end {binaryFskAnalyzerSettings.WindowPositionEndMicroseconds:N3} us, window position increment {binaryFskAnalyzerSettings.WindowPositionIncrementMicroseconds:N3} us");
            Console.WriteLine($"Window length start {binaryFskAnalyzerSettings.WindowLengthStartMicroseconds:N3} us, window length end {binaryFskAnalyzerSettings.WindowLengthEndMicroseconds:N3} us, window length increment {binaryFskAnalyzerSettings.WindowLengthIncrementMicroseconds:N3} us");
            Console.WriteLine();

            var result = binaryFskAnalyzer.AnalyzeSignal();
            var bits   = new List <bool>();

            foreach (var frame in result.AnalysisFrames)
            {
                if (frame.Bit.HasValue == true)
                {
                    bits.Add(frame.Bit.Value);
                }
            }

            Console.WriteLine("Rendering bytes");
            Console.WriteLine();
            var renderer = (IRenderer) new RowRenderer();

            renderer.Render(BitManipulator.BitsToBytes(bits));
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Rendering ASCII");
            Console.WriteLine();
            renderer = new AsciiRenderer();
            renderer.Render(BitManipulator.BitsToBytes(bits));
            Console.WriteLine();

            Console.WriteLine("Done");
            Console.ReadLine();
        }