/// <summary> /// Gets an AudioClip for a song /// </summary> /// <remarks> /// Run this from the main thread and only from the main thread! /// </remarks> public async Task <RefCountedClip> GetSongClip(string song, CancellationToken token) { if (!AsyncUtils.IsOnMainThread()) { throw new InvalidOperationException("GetSongClip must be run from the main thread. I don't make the rules, sorry."); } if (State.ClipCache.ContainsKey(song)) { return(State.ClipCache[song]); } //request the server transcode var request = new RestRequest($"library/song/{song}", HttpMethod.Get, null, new KeyValuePair <string, string>("transcode", "wave"), new KeyValuePair <string, string>("return", "path")); RestResponse response = default; await Task.Run(async() => response = await DoRestRequest(request, 100000, token)); var jroot = JToken.Parse(response.Body); if (jroot.IsNullOrEmpty() || jroot["data"].IsNullOrEmpty() || jroot["data"]["transcodedPath"].IsNullOrEmpty()) { return(null); } string path = jroot["data"]["transcodedPath"].ToString(); //load the clip var clip = WaveLoader.WaveLoader.LoadWaveToAudioClip(path, song); //will probably stutter, we'll optimize it later var rClip = new RefCountedClip(clip); State.ClipCache.Add(song, rClip); return(rClip); }