<commit_before>﻿namespace Macabre2D.Examples.AudioTest {

    using Macabre2D.Framework;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Input;
    using System;
    using System.Collections.Generic;
    using System.Threading;

    public sealed class SynthTestComponent : BaseComponent, IUpdateableComponent {
        private const int SampleRate = 44100 / 4;
        private const int SamplesPerBuffer = 500;
        private readonly byte[] _audioBuffer = new byte[SamplesPerBuffer * 2];
        private readonly float[] _workiingBuffer = new float[SamplesPerBuffer];
        private DynamicSoundEffectInstance _instance;
        private Queue<IOscillator> _oscillators = new Queue<IOscillator>();
        private IOscillator _selectedOscillator;
        private float _time = 0f;

        public override void LoadContent() {
            var pulseWaveOscillator = new PulseWaveOscillator() {
                DutyCycle = 0.1f
            };

            this._selectedOscillator = new SineWaveOscillator();
            this._oscillators.Enqueue(new TriangleWaveOscillator());
            this._oscillators.Enqueue(new SawToothOscillator());
            this._oscillators.Enqueue(new SquareWaveOscillator());
            this._oscillators.Enqueue(pulseWaveOscillator);
            this._oscillators.Enqueue(new WhiteNoiseOscillator());

            this._instance = new DynamicSoundEffectInstance(SampleRate, AudioChannels.Mono);
            this._instance.BufferNeeded += this._instance_BufferNeeded;
            this._instance.Play();
            base.LoadContent();
        }

        public void Update(GameTime gameTime) {
            var frequency = 0f;
            var keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.Right)) {
                this._oscillators.Enqueue(this._selectedOscillator);
                this._selectedOscillator = this._oscillators.Dequeue();
                Thread.Sleep(200);
            }

            if (keyboardState.IsKeyDown(Keys.D1)) {
                frequency = MusicalScale.C.ToFrequency(MusicalPitch.Low);
            }
            else if (keyboardState.IsKeyDown(Keys.D2)) {
                frequency = MusicalScale.F.ToFrequency(MusicalPitch.Low);
            }
            else if (keyboardState.IsKeyDown(Keys.D3)) {
                frequency = MusicalScale.G.ToFrequency(MusicalPitch.Low);
            }
            else {
                this._time = 0f;
            }

            if (frequency > 0) {
                while (this._instance.PendingBufferCount < 2) {
                    Console.WriteLine(frequency);
                    this.SubmitBuffer(frequency);
                }
            }
        }

        protected override void Initialize() {
        }

        private void _instance_BufferNeeded(object sender, EventArgs e) {
        }

        private void ConvertBuffer() {
            for (int sampleIndex = 0; sampleIndex < SamplesPerBuffer; sampleIndex++) {
                var workingSample = MathHelper.Clamp(this._workiingBuffer[sampleIndex], -1f, 1f);
                var shortSample = (short)Math.Round(workingSample >= 0f ? workingSample * short.MaxValue : workingSample * short.MinValue * -1);
                var index = sampleIndex * 2;
                //Console.WriteLine($"{this._workiingBuffer[sampleIndex]} - {workingSample} - {shortSample}");

                if (!BitConverter.IsLittleEndian) {
                    this._audioBuffer[index] = (byte)(shortSample >> 8);
                    this._audioBuffer[index + 1] = (byte)(shortSample);
                }
                else {
                    this._audioBuffer[index] = (byte)(shortSample);
                    this._audioBuffer[index + 1] = (byte)(shortSample >> 8);
                }
            }
        }

        private void FillWorkingBuffer(float frequency) {
            for (var i = 0; i < SamplesPerBuffer; i++) {
                this._workiingBuffer[i] = this._selectedOscillator.GetSignal(this._time, frequency, 0.8f);

                this._time += 1.0f / SampleRate;
            }
        }

        private void SubmitBuffer(float frequency) {
            this.FillWorkingBuffer(frequency);
            this.ConvertBuffer();<commit_msg>Refactor: cleaning up synth test component.<commit_after>﻿namespace Macabre2D.Examples.AudioTest {

    using Macabre2D.Framework;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Input;
    using System;
    using System.Collections.Generic;
    using System.Threading;

    public sealed class SynthTestComponent : BaseComponent, IUpdateableComponent {
        private const int SampleRate = 44100 / 4;
        private const int SamplesPerBuffer = 500;
        private readonly byte[] _audioBuffer = new byte[SamplesPerBuffer * 2];
        private readonly float[] _workiingBuffer = new float[SamplesPerBuffer];
        private DynamicSoundEffectInstance _instance;
        private Queue<IOscillator> _oscillators = new Queue<IOscillator>();
        private IOscillator _selectedOscillator;
        private float _time = 0f;

        public override void LoadContent() {
            var pulseWaveOscillator = new PulseWaveOscillator() {
                DutyCycle = 0.1f
            };

            this._selectedOscillator = new SineWaveOscillator();
            this._oscillators.Enqueue(new TriangleWaveOscillator());
            this._oscillators.Enqueue(new SawToothOscillator());
            this._oscillators.Enqueue(new SquareWaveOscillator());
            this._oscillators.Enqueue(pulseWaveOscillator);
            this._oscillators.Enqueue(new WhiteNoiseOscillator());

            this._instance = new DynamicSoundEffectInstance(SampleRate, AudioChannels.Mono);
            this._instance.BufferNeeded += this._instance_BufferNeeded;
            this._instance.Play();
            base.LoadContent();
        }

        public void Update(GameTime gameTime) {
            var frequency = 0f;
            var keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.Right)) {
                this._oscillators.Enqueue(this._selectedOscillator);
                this._selectedOscillator = this._oscillators.Dequeue();
                Thread.Sleep(200);
            }

            if (keyboardState.IsKeyDown(Keys.D1)) {
                frequency = MusicalScale.C.ToFrequency(MusicalPitch.Middle);
            }
            else if (keyboardState.IsKeyDown(Keys.D2)) {
                frequency = MusicalScale.F.ToFrequency(MusicalPitch.Middle);
            }
            else if (keyboardState.IsKeyDown(Keys.D3)) {
                frequency = MusicalScale.G.ToFrequency(MusicalPitch.Middle);
            }
            else {
                this._time = 0f;
            }

            if (frequency > 0) {
                while (this._instance.PendingBufferCount < 2) {
                    Console.WriteLine(frequency);
                    this.SubmitBuffer(frequency);
                }
            }
        }

        protected override void Initialize() {
        }

        private void ConvertBuffer() {
            for (var sampleIndex = 0; sampleIndex < SamplesPerBuffer; sampleIndex++) {
                var workingSample = MathHelper.Clamp(this._workiingBuffer[sampleIndex], -1f, 1f);
                var shortSample = (short)Math.Round(workingSample >= 0f ? workingSample * short.MaxValue : workingSample * short.MinValue * -1);
                var index = sampleIndex * 2;

                if (!BitConverter.IsLittleEndian) {
                    this._audioBuffer[index] = (byte)(shortSample >> 8);
                    this._audioBuffer[index + 1] = (byte)(shortSample);
                }
                else {
                    this._audioBuffer[index] = (byte)(shortSample);
                    this._audioBuffer[index + 1] = (byte)(shortSample >> 8);
                }
            }
        }

        private void FillWorkingBuffer(float frequency) {
            for (var i = 0; i < SamplesPerBuffer; i++) {
                this._workiingBuffer[i] = this._selectedOscillator.GetSignal(this._time, frequency, 1f);

                this._time += 1.0f / SampleRate;
            }
        }

        private void SubmitBuffer(float frequency) {
            this.FillWorkingBuffer(frequency);
            this.ConvertBuffer();
{'fraction_non_alphanumeric': 0.09316107621192367, 'fraction_numerical': 0.00936351783809411, 'mean_word_length': 1.9921985815602836, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6801933', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2155, 'n_words': 419}