/// <summary> /// Constructor /// <para>Initialises all fields.</para> /// </summary> /// <param name="song">Song that was serialised from the "SongName.xml".</param> public UtilityHelper(Song song, ProgressBar progressBar) { //Init Buffer AudioBuffer = new AudioBuffer(); ResultBuffer = new ResultBuffer(); //Init Classes AudioRecording = new AudioRecording(AudioBuffer); API_Helper = new API_Helper(AudioBuffer, ResultBuffer, ConfigFileHelper.ConfigApiAddress); DataAnalyzer = new DataAnalyzer(ResultBuffer); SongHelper.InitHelper(song); SongHelper.ProgressMade += OnProgressMade; _progressBar = progressBar; cts = new CancellationTokenSource(); }
/// <summary> /// Records audio data until the temporary buffer is full. The data is then added to the <see cref="AudioBuffer"/>/> /// </summary> /// <param name="ct">Token that throws an OperationCancelledException when the token state is set to cancel. Used to stop the task.</param> /// <returns>Task, so the method can be run asynchronously</returns> private async Task ReadDataToBuffer(CancellationToken ct) { ct.ThrowIfCancellationRequested(); SafeGetRecorder().StartRecording(); while (!ct.IsCancellationRequested) { //if (DevFlags.LoggingEnabled) Logger.RecordingLog("Looping"); await SafeGetRecorder().ReadAsync(InputBuffer, 0, InputBuffer.Length, 0); RecordCount++; AudioBuffer?.Add(new AudioData(InputBuffer, TimeHelper.GetElapsedTime())); InputBuffer = new float[InputBuffer.Length]; if ((TimeHelper.GetElapsedTime() - 1000) > SongHelper.SongDuration) { Logger.RecordingLog("Duration of sog reached => stopping"); break; } } SafeGetRecorder()?.Stop(); ct.ThrowIfCancellationRequested(); }
/// <summary> /// Loop that reads the first entrance of the <see cref="AudioBuffer"/> and makes the API call. /// The data that is recieved is pushed into the <see cref="ResultBuffer"/>./> /// </summary> /// <param name="ct">Token that throws an OperationCancelledException when the token state is set to cancel. Used to stop the task.</param> /// <returns>Task, so the method can be run asynchronously.</returns> private async Task ProcessingCycle(CancellationToken ct) { using (HttpClient httpClient = new HttpClient()) { httpClient.Timeout = new TimeSpan(0, 0, 12); while (true) { //if (DevFlags.LoggingEnabled) Logger.APILog("Looping"); ct.ThrowIfCancellationRequested(); if (AudioBuffer?.Peek() != null) { var data = AudioBuffer?.Get(); if (data != null) { EssentiaModel essentiaModel = new EssentiaModel(data.Data, data.Time); var chordData = await DoAPICall(essentiaModel, httpClient, ct); ResultBuffer.Add(chordData); } } } } }
/// <summary> /// Clean both buffers. /// </summary> private void EmptyBuffer() { AudioBuffer.Clean(); ResultBuffer.Clean(); }
/// <summary> /// Frees the memory used by the <see cref="AudioRecorder"/> and empties the buffer. /// </summary> public void CleanUp() { SafeGetRecorder()?.Release(); AudioBuffer?.Clean(); }