From 17639a9b548656db9047b8f371864bcbb7ae9953 Mon Sep 17 00:00:00 2001 From: Will Graham Date: Sun, 2 Jul 2017 13:37:11 -0400 Subject: [PATCH] Add Audio Track Analysis models and API endpoint (#161) * Add Audio Track Analysis models and API endpoint * Update docs * Add link to EchoNest archived docs for AudioAnalysis model --- SpotifyAPI.Docs/docs/SpotifyWebAPI/tracks.md | 19 +++++ SpotifyAPI/SpotifyAPI.csproj | 6 ++ SpotifyAPI/Web/Models/AnalysisMeta.cs | 28 +++++++ SpotifyAPI/Web/Models/AnalysisSection.cs | 43 ++++++++++ SpotifyAPI/Web/Models/AnalysisSegment.cs | 35 ++++++++ SpotifyAPI/Web/Models/AnalysisTimeSlice.cs | 16 ++++ SpotifyAPI/Web/Models/AnalysisTrack.cs | 86 ++++++++++++++++++++ SpotifyAPI/Web/Models/AudioAnalysis.cs | 29 +++++++ SpotifyAPI/Web/SpotifyWebAPI.cs | 22 +++++ SpotifyAPI/Web/SpotifyWebBuilder.cs | 11 +++ 10 files changed, 295 insertions(+) create mode 100644 SpotifyAPI/Web/Models/AnalysisMeta.cs create mode 100644 SpotifyAPI/Web/Models/AnalysisSection.cs create mode 100644 SpotifyAPI/Web/Models/AnalysisSegment.cs create mode 100644 SpotifyAPI/Web/Models/AnalysisTimeSlice.cs create mode 100644 SpotifyAPI/Web/Models/AnalysisTrack.cs create mode 100644 SpotifyAPI/Web/Models/AudioAnalysis.cs diff --git a/SpotifyAPI.Docs/docs/SpotifyWebAPI/tracks.md b/SpotifyAPI.Docs/docs/SpotifyWebAPI/tracks.md index 36e41f82..6a56b6ef 100644 --- a/SpotifyAPI.Docs/docs/SpotifyWebAPI/tracks.md +++ b/SpotifyAPI.Docs/docs/SpotifyWebAPI/tracks.md @@ -38,3 +38,22 @@ Console.WriteLine(track.Name); ``` --- +##GetAudioAnalysis + +> Get a detailed audio analysis for a single track identified by its unique Spotify ID. + +**Paramters** + +|Name|Description|Example| +|--------------|-------------------------|-------------------------| +|id| The Spotify ID for the track. | `"6Y1CLPwYe7zvI8PJiWVz6T"` + +Returns a AudioAnalysis. This object is currently lacking Spotify documentation but archived [EchoNest documentation](https://web.archive.org/web/20160528174915/http://developer.echonest.com/docs/v4/_static/AnalyzeDocumentation.pdf) is relevant. + +**Usage** +```cs +AudioAnalysis analysis = _spotify.GetAudioAnalysis("6Y1CLPwYe7zvI8PJiWVz6T"); +Console.WriteLine(analysis.Meta.DetailedStatus); +``` + +--- \ No newline at end of file diff --git a/SpotifyAPI/SpotifyAPI.csproj b/SpotifyAPI/SpotifyAPI.csproj index df4bd6e2..dfd34d3b 100644 --- a/SpotifyAPI/SpotifyAPI.csproj +++ b/SpotifyAPI/SpotifyAPI.csproj @@ -80,7 +80,13 @@ + + + + + + diff --git a/SpotifyAPI/Web/Models/AnalysisMeta.cs b/SpotifyAPI/Web/Models/AnalysisMeta.cs new file mode 100644 index 00000000..bb9b7774 --- /dev/null +++ b/SpotifyAPI/Web/Models/AnalysisMeta.cs @@ -0,0 +1,28 @@ +using Newtonsoft.Json; + +namespace SpotifyAPI.Web.Models +{ + public class AnalysisMeta + { + [JsonProperty("analyzer_platform")] + public string AnalyzerVersion { get; set; } + + [JsonProperty("platform")] + public string Platform { get; set; } + + [JsonProperty("status_code")] + public int StatusCode { get; set; } + + [JsonProperty("detailed_status")] + public string DetailedStatus { get; set; } + + [JsonProperty("timestamp")] + public long Timestamp { get; set; } + + [JsonProperty("analysis_time")] + public double AnalysisTime { get; set; } + + [JsonProperty("input_process")] + public string InputProcess { get; set; } + } +} diff --git a/SpotifyAPI/Web/Models/AnalysisSection.cs b/SpotifyAPI/Web/Models/AnalysisSection.cs new file mode 100644 index 00000000..bcb92bc2 --- /dev/null +++ b/SpotifyAPI/Web/Models/AnalysisSection.cs @@ -0,0 +1,43 @@ +using Newtonsoft.Json; + +namespace SpotifyAPI.Web.Models +{ + public class AnalysisSection + { + [JsonProperty("start")] + public double Start { get; set; } + + [JsonProperty("duration")] + public double Duration { get; set; } + + [JsonProperty("confidence")] + public double Confidence { get; set; } + + [JsonProperty("loudness")] + public double Loudness { get; set; } + + [JsonProperty("tempo")] + public double Tempo { get; set; } + + [JsonProperty("tempo_confidence")] + public double TempoConfidence { get; set; } + + [JsonProperty("key")] + public int Key { get; set; } + + [JsonProperty("key_confidence")] + public double KeyConfidence { get; set; } + + [JsonProperty("mode")] + public int Mode { get; set; } + + [JsonProperty("mode_confidence")] + public double ModeConfidence { get; set; } + + [JsonProperty("time_signature")] + public int TimeSignature { get; set; } + + [JsonProperty("time_signature_confidence")] + public double TimeSignatureConfidence { get; set; } + } +} diff --git a/SpotifyAPI/Web/Models/AnalysisSegment.cs b/SpotifyAPI/Web/Models/AnalysisSegment.cs new file mode 100644 index 00000000..bdf5270b --- /dev/null +++ b/SpotifyAPI/Web/Models/AnalysisSegment.cs @@ -0,0 +1,35 @@ +using Newtonsoft.Json; +using System.Collections.Generic; + +namespace SpotifyAPI.Web.Models +{ + public class AnalysisSegment + { + [JsonProperty("start")] + public double Start { get; set; } + + [JsonProperty("duration")] + public double Duration { get; set; } + + [JsonProperty("confidence")] + public double Confidence { get; set; } + + [JsonProperty("loudness_start")] + public double LoudnessStart { get; set; } + + [JsonProperty("loudness_max_time")] + public double LoudnessMaxTime { get; set; } + + [JsonProperty("loudness_max")] + public double LoudnessMax { get; set; } + + [JsonProperty("loudness_end")] + public double LoudnessEnd { get; set; } + + [JsonProperty("pitches")] + public List Pitches { get; set; } + + [JsonProperty("timbre")] + public List Timbre { get; set; } + } +} diff --git a/SpotifyAPI/Web/Models/AnalysisTimeSlice.cs b/SpotifyAPI/Web/Models/AnalysisTimeSlice.cs new file mode 100644 index 00000000..41ab5f15 --- /dev/null +++ b/SpotifyAPI/Web/Models/AnalysisTimeSlice.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace SpotifyAPI.Web.Models +{ + public class AnalysisTimeSlice + { + [JsonProperty("start")] + public double Start { get; set; } + + [JsonProperty("duration")] + public double Duration { get; set; } + + [JsonProperty("confidence")] + public double Confidence { get; set; } + } +} diff --git a/SpotifyAPI/Web/Models/AnalysisTrack.cs b/SpotifyAPI/Web/Models/AnalysisTrack.cs new file mode 100644 index 00000000..e84bc629 --- /dev/null +++ b/SpotifyAPI/Web/Models/AnalysisTrack.cs @@ -0,0 +1,86 @@ +using Newtonsoft.Json; + +namespace SpotifyAPI.Web.Models +{ + public class AnalysisTrack + { + [JsonProperty("num_samples")] + public int NumSamples { get; set; } + + [JsonProperty("duration")] + public double Duration { get; set; } + + [JsonProperty("sample_md5")] + public string SampleMD5 { get; set; } + + [JsonProperty("offset_seconds")] + public double OffsetSeconds { get; set; } + + [JsonProperty("window_seconds")] + public double WindowSeconds { get; set; } + + [JsonProperty("analysis_sample_rate")] + public int AnalysisSampleRate { get; set; } + + [JsonProperty("analysis_channels")] + public int AnalysisChannels { get; set; } + + [JsonProperty("end_of_fade_in")] + public double EndOfFadeIn { get; set; } + + [JsonProperty("start_of_fade_out")] + public double StartOfFadeOut { get; set; } + + [JsonProperty("loudness")] + public double Loudness { get; set; } + + [JsonProperty("tempo")] + public double Tempo { get; set; } + + [JsonProperty("tempo_confidence")] + public double TempoConfidence { get; set; } + + [JsonProperty("time_signature")] + public double TimeSignature { get; set; } + + [JsonProperty("time_signature_confidence")] + public double TimeSignatureConfidence { get; set; } + + [JsonProperty("key")] + public int Key { get; set; } + + [JsonProperty("key_confidence")] + public double KeyConfidence { get; set; } + + [JsonProperty("mode")] + public int Mode { get; set; } + + [JsonProperty("mode_confidence")] + public double ModeConfidence { get; set; } + + [JsonProperty("codestring")] + public string Codestring { get; set; } + + [JsonProperty("code_version")] + public double CodeVersion { get; set; } + + [JsonProperty("echoprintstring")] + public string Echoprintstring { get; set; } + + [JsonProperty("echoprint_version")] + public double EchoprintVersion { get; set; } + + [JsonProperty("synchstring")] + public string Synchstring { get; set; } + + [JsonProperty("synch_version")] + public double SynchVersion { get; set; } + + [JsonProperty("rhythmstring")] + public string Rhythmstring { get; set; } + + [JsonProperty("rhythm_version")] + public double RhythmVersion { get; set; } + + } +} diff --git a/SpotifyAPI/Web/Models/AudioAnalysis.cs b/SpotifyAPI/Web/Models/AudioAnalysis.cs new file mode 100644 index 00000000..bc8f622b --- /dev/null +++ b/SpotifyAPI/Web/Models/AudioAnalysis.cs @@ -0,0 +1,29 @@ +using Newtonsoft.Json; +using System.Collections.Generic; + +namespace SpotifyAPI.Web.Models +{ + public class AudioAnalysis : BasicModel + { + [JsonProperty("bars")] + public List Bars { get; set; } + + [JsonProperty("beats")] + public List Beats { get; set; } + + [JsonProperty("meta")] + public AnalysisMeta Meta { get; set; } + + [JsonProperty("sections")] + public List Sections { get; set; } + + [JsonProperty("segments")] + public List Segments { get; set; } + + [JsonProperty("tatums")] + public List Tatums { get; set; } + + [JsonProperty("track")] + public AnalysisTrack Track { get; set; } + } +} diff --git a/SpotifyAPI/Web/SpotifyWebAPI.cs b/SpotifyAPI/Web/SpotifyWebAPI.cs index c41e562b..54a7c341 100644 --- a/SpotifyAPI/Web/SpotifyWebAPI.cs +++ b/SpotifyAPI/Web/SpotifyWebAPI.cs @@ -1780,6 +1780,28 @@ namespace SpotifyAPI.Web return DownloadDataAsync(_builder.GetTrack(id, market)); } + /// + /// Get a detailed audio analysis for a single track identified by its unique Spotify ID. + /// + /// The Spotify ID for the track. + /// + /// AUTH NEEDED + public AudioAnalysis GetAudioAnalysis(string id) + { + return DownloadData(_builder.GetAudioAnalysis(id)); + } + + /// + /// Get a detailed audio analysis for a single track identified by its unique Spotify ID asynchronously. + /// + /// The Spotify ID for the track. + /// + /// AUTH NEEDED + public Task GetAudioAnalysisAsync(string id) + { + return DownloadDataAsync(_builder.GetAudioAnalysis(id)); + } + /// /// Get audio feature information for a single track identified by its unique Spotify ID. /// diff --git a/SpotifyAPI/Web/SpotifyWebBuilder.cs b/SpotifyAPI/Web/SpotifyWebBuilder.cs index 094eff6e..84a197f1 100644 --- a/SpotifyAPI/Web/SpotifyWebBuilder.cs +++ b/SpotifyAPI/Web/SpotifyWebBuilder.cs @@ -817,6 +817,17 @@ namespace SpotifyAPI.Web return $"{APIBase}/tracks/{id}?market={market}"; } + /// + /// Get a detailed audio analysis for a single track identified by its unique Spotify ID. + /// + /// The Spotify ID for the track. + /// + /// AUTH NEEDED + public string GetAudioAnalysis(string id) + { + return $"{APIBase}/audio-analysis/{id}"; + } + /// /// Get audio feature information for a single track identified by its unique Spotify ID. ///