/// <summary>
        /// Splits the data string that was returned by the API and the most propable note.
        /// </summary>
        /// <param name="model">API data</param>
        /// <returns>String, name of the note that occured the most.</returns>
        private static string GetNoteFromData(EssentiaModel model)
        {
            string[] chords    = model.chordData.Split(';');
            var      occurence = GetOccurence(chords);

            return(GetChordFromOccurence(occurence));
        }
Esempio n. 2
0
 /// <summary>
 /// Add new data to the queue.
 /// </summary>
 /// <param name="model"><see cref="EssentiaModel"/> to add to the buffer.</param>
 public void Add(EssentiaModel model)
 {
     lock (_lock)
     {
         if (model == null)
         {
             return;
         }
         Buffer.Enqueue(model);
     }
 }
        /// <summary>
        /// Compares the Essentia data with the musical data based on timing and value of note. Uses the <see cref="SongHelper"/>.
        /// </summary>
        /// <param name="essentiaData">Data that shall be compared with the current note.</param>
        /// <returns></returns>
        public SongObject CompareWithSheet(EssentiaModel essentiaData)
        {
            string     noteToCompare = GetNoteFromData(essentiaData);
            SongObject songObject    = SongHelper.GetNext();

            //SongHelper.GetNext() sometimes returns an invalid object
            //Temporary Hack (not even working...)
            if (songObject == null)
            {
                if (DevFlags.LoggingEnabled)
                {
                    Logger.AnalyzerLog("songObject couldn't be loaded - " + songObject.ToString());
                }
                return(null);
            }

            //if(DevFlags.LoggingEnabled) Logger.AnalyzerLog("Comparing: " + songObject.Name + " - " + noteToCompare);
            if (CompareNamesOf(songObject.Name, noteToCompare) &&
                InRange(songObject.TimePosition, essentiaData.time))
            {
                if (DevFlags.LoggingEnabled)
                {
                    Logger.AnalyzerLog("Equal");
                }
            }
            else
            {
                songObject.Type = Highlight.None;
                if (DevFlags.LoggingEnabled)
                {
                    Logger.AnalyzerLog("Not equal");
                }
            }

            if (SongHelper.IncreaseIndex(essentiaData.time, songObject))
            {
                //End of Song
                InfoContainer.DisplayResults();
                Logger.Log("Analyzer: END OF SONG, now crashing?");
                throw new Exception("End of Song");
            }

            return(songObject);
        }
 public EssentiaModel Post(EssentiaModel input)
 {
     try
     {
         //Logger.Log("IncomingData: " + input.chordData + " - " + input.audioData.Length + " - " + input.audioData.ToString());
         string chordData = EssentiaInterface.CalculateChordsFrom(input.audioData);
         if (chordData == "")
         {
             chordData = "X,0.0;";
         }
         return(new EssentiaModel()
         {
             audioData = new float[1], chordData = chordData, time = input.time
         });
     }
     catch (Exception e)
     {
         Logger.Log("Error - " + e.Message + "\nStack: " + e.StackTrace);
         return(null);
     }
 }
        /// <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>
        /// Implementation of the actual communication with the API used by <see cref="ProcessingCycle(CancellationToken)"/>.
        /// </summary>
        /// <param name="currentModel">Data that will be sent to the API.</param>
        /// <param name="myClient">HttpClient that is used for the communication.</param>
        /// <param name="ct">Token that throws an OperationCancelledException when the token state is set to cancel. Used to stop the task.</param>
        /// <returns>Task[EssentiaModel], method can be run asynchronously. Actual return data is the EssentiaModel</returns>
        private async Task <EssentiaModel> DoAPICall(EssentiaModel currentModel, HttpClient myClient, CancellationToken ct)
        {
            ct.ThrowIfCancellationRequested();
            EssentiaModel apiModel = null;

            using (HttpRequestMessage request = new HttpRequestMessage())
            {
                request.RequestUri = new Uri(ApiAddress);
                request.Method     = HttpMethod.Post;
                request.Headers.Add("Accept", "application/json");
                var requestContent = JsonConvert.SerializeObject(currentModel);
                request.Content = new StringContent(requestContent, Encoding.UTF8, "application/json");

                ct.ThrowIfCancellationRequested();
                HttpResponseMessage response = await myClient.SendAsync(request);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    ResponsesRecieved++;
                    HttpContent content = response.Content;
                    string      json    = await content.ReadAsStringAsync();

                    apiModel = JsonConvert.DeserializeObject <EssentiaModel>(json);
                    if (DevFlags.LoggingEnabled)
                    {
                        Logger.APILog("Response == OK -" + apiModel.chordData);
                    }
                }
                else if (DevFlags.LoggingEnabled)
                {
                    Logger.APILog("Response != OK"); ErrorResponses++;
                }
            }
            ct.ThrowIfCancellationRequested();
            return(apiModel);
        }