spotify_rs/model/
audio.rs

1use serde::Deserialize;
2use serde_repr::Deserialize_repr;
3
4/// Audio features for a track.
5#[derive(Clone, Debug, Deserialize, PartialEq)]
6pub struct AudioFeatures {
7    /// A measure of confidence from `0.0` to `1.0` indicating whether the track is acoustic.
8    /// A score of `1.0` implies high certainty that the track is acoustic.
9    pub acousticness: f32,
10    /// A URL to access the full audio analysis of the track.
11    pub analysis_url: String,
12    /// A measure between `0.0` - `1.0` that describes how suitable a track is for
13    /// dancing based on various musical factors like tempo, rhythm stability,
14    /// beat strength, and overall regularity.
15    pub danceability: f32,
16    /// The duration of the track in miliseconds.
17    pub duration_ms: u32,
18    /// A score between `0.0` - `1.0` that represents the intensity and activity
19    /// level of a track.
20    ///
21    /// Energetic tracks feel fast, loud, and lively. For instance, death metal
22    /// scores high in energy, while a Bach prelude scores low.
23    ///
24    /// Features contributing to this include dynamic range,
25    /// perceived loudness, timbre, onset rate, and overall entropy.
26    pub energy: f32,
27    /// The [Spotify ID](https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids) for the track.
28    pub id: String,
29    /// A value between `0.0` - `1.0` that estimates the likelihood of a track being
30    /// instrumental (no vocals).
31    ///
32    /// 'Ooh' and 'aah' sounds are considered instrumental. Rap or spoken word
33    /// tracks are considered vocal.
34    ///
35    /// Values above `0.5` suggest instrumental tracks, but confidence is higher
36    /// as the value nears `1.0`.
37    pub instrumentalness: f32,
38    /// A value ranging between `-1` - `11` that denotes musical key of the track,
39    /// represented by integers mapping to pitches using standard
40    /// [Pitch Class notation] (https://en.wikipedia.org/wiki/Pitch_class).
41    ///
42    /// If no key is detected, the value is `-1`.
43    pub key: i32,
44    /// A value ranging between `0.0` and `1.0` that measures the likelihood
45    /// of the presence of an audience in the recording.
46    ///
47    /// A value above `0.8` strongly suggests that the track is live.
48    pub liveness: f32,
49    /// The average loudness of the track in decibels (dB). Loudness values
50    /// are averaged across the entire track and are useful for comparing
51    /// relative loudness of tracks.
52    ///
53    /// Values typically range between `-60` and `0` dB.
54    pub loudness: f32,
55    /// Indicates the modality (major or minor) of the track.
56    pub mode: Mode,
57    /// A value between `0.0` - `1.0` that detects the presence of spoken words in
58    /// a track. A value closer to 1.0 indicates more speech-like content.
59    ///
60    /// Values below `0.33` most likely represent music.
61    ///
62    /// Values between `0.33` - `0.66` describe tracks that may contain both music and speech,
63    /// either in sections or layered.
64    ///
65    /// Values above `0.66` most likely represent tracks that consist entirely of
66    /// spoken words, like podcasts, audiobooks etc.
67    pub speechiness: f32,
68    /// The estimated pace of the track in beats per minute (BPM).
69    pub tempo: f32,
70    /// An estimated notation of how many beats are in each measure.
71    ///
72    /// Values range between `3` - `7`, indicating time signatures ranging
73    /// between `3/4` - `7/4`.
74    pub time_signature: u32,
75    /// A link to the Spotify Web API endpoint providing full details of the track.
76    pub track_href: String,
77    /// The object type. Allowed values: `audio_features`.
78    pub r#type: String,
79    /// The [Spotify URI](https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids)
80    /// for the track.
81    pub uri: String,
82    /// A measure from `0.0` to `1.0` describing the musical positiveness
83    /// conveyed by a track.
84    ///
85    /// High valence tracks sound more positive (e.g. happy, cheerful), while
86    /// low valence tracks sound more negative (e.g. sad, depressed, angry).
87    pub valence: f32,
88}
89
90// Used only to deserialize JSON responses with arrays that are named objects.
91#[derive(Clone, Debug, Deserialize, PartialEq)]
92pub(crate) struct AudioFeaturesList {
93    pub(crate) audio_features: Vec<Option<AudioFeatures>>,
94}
95
96/// Audio analysis for a track.
97#[derive(Clone, Debug, Deserialize, PartialEq)]
98pub struct AudioAnalysis {
99    pub meta: Meta,
100    pub track: TrackAnalysis,
101    /// The time intervals of the measures throughout the track. A measure
102    /// (or bar) is a segment of time defined by a specific number of beats.
103    pub bars: Vec<Bar>,
104    /// The timing intervals of beats throughout the track. A beat is the
105    /// fundamental time unit of a piece of music; for instance, each tick
106    /// of a metronome. Beats are usually multiples of tatums.
107    pub beats: Vec<Beat>,
108    /// Sections are defined by significant changes in rhythm or timbre,
109    /// such as the chorus, verse, bridge, guitar solo, etc.
110    ///
111    /// Each section has its own descriptions of tempo, key, mode,
112    /// time signature, and loudness.
113    pub sections: Vec<Section>,
114    /// Each segment contains a relatively consistent sound throughout its duration.
115    pub segments: Vec<Segment>,
116    /// A tatum represents the smallest regular pulse that a listener naturally
117    /// infers from the timing of perceived musical events (segments).
118    pub tatums: Vec<Tatum>,
119}
120
121#[derive(Clone, Debug, Deserialize, PartialEq)]
122pub struct Meta {
123    /// The version of the analyser used to analyse the track.
124    pub analyzer_version: String,
125    /// The platform used to read the track's audio data (e.g. Linux).
126    pub platform: String,
127    /// A status code for the track. If analysis data is missing,
128    /// the status code may explain why.
129    pub detailed_status: String,
130    /// The return code of the analyser process.
131    pub status_code: AnalysisStatusCode,
132    /// The Unix timestamp at which the track was analysed.
133    pub timestamp: u64,
134    /// The amount of time taken to analyse the track.
135    pub analysis_time: f32,
136    /// The method used to read the track's audio data.
137    pub input_process: String,
138}
139
140#[derive(Clone, Debug, Deserialize, PartialEq)]
141pub struct TrackAnalysis {
142    /// The number of audio samples from the track that were analysed.
143    pub num_samples: u32,
144    /// The length of the track in seconds.
145    pub duration: f32,
146    /// This field will always contain an empty string.
147    pub sample_md5: String,
148    /// An offset to the start of the segment of the track that awas analysed.
149    /// As the entire track is analysed, this should be `0`.
150    pub offset_seconds: u32,
151    /// The length of the segment of the track that was analysed, if only a subset
152    /// was. As the entire track is analysed, this should be `0`.
153    pub window_seconds: u32,
154    /// The sample rate used to decode and analyse the track. May vary from the
155    /// track's sample rate on Spotify.
156    pub analysis_sample_rate: u32,
157    /// The number of channels used in the analysis. If the value is `1`, all
158    /// the channels were combined to mono before analysis.
159    pub analysis_channels: u32,
160    /// The time, in seconds, when the track's fade-in ends. If the track has no
161    /// fade-in, the value is `0`.
162    pub end_of_fade_in: f32,
163    /// The time, in seconds, when the track's fade-out begins. If the track has
164    /// no fade-out, this will match the track's length.
165    pub start_of_fade_out: f32,
166    /// The average loudness of the track in decibels (dB). Loudness values
167    /// are averaged across the entire track and are useful for comparing
168    /// relative loudness of tracks.
169    ///
170    /// Values typically range between `-60` and `0` dB.
171    pub loudness: f32,
172    /// The estimated pace of the track in beats per minute (BPM).
173    pub tempo: f32,
174    /// A value ranging between `0.0` - `1.0` that indicates the confidence
175    /// of the `tempo`.
176    pub tempo_confidence: f32,
177    /// An estimated notation of how many beats are in each measure.
178    ///
179    /// Values range between `3` - `7`, indicating time signatures ranging
180    /// between `3/4` - `7/4`.
181    pub time_signature: u32,
182    /// A value ranging between `0.0` - `1.0` that indicates the confidence
183    /// of the `time_signature`.
184    pub time_signature_confidence: f32,
185    /// A value ranging between `-1` - `11` that denotes musical key of the track,
186    /// represented by integers mapping to pitches using standard
187    /// [Pitch Class notation] (https://en.wikipedia.org/wiki/Pitch_class).
188    ///
189    /// If no key is detected, the value is `-1`.
190    pub key: i32,
191    /// A value ranging between `0.0` - `1.0` that indicates the confidence
192    /// of the `key`.
193    pub key_confidence: f32,
194    /// Indicates the modality (major or minor) of a track.
195    pub mode: Mode,
196    /// A value ranging between `0.0` - `1.0` that indicates the confidence
197    /// of the `mode`.
198    pub mode_confidence: f32,
199    /// An [ENMPF](https://academiccommons.columbia.edu/doi/10.7916/D8Q248M4)
200    /// codestring for the track.
201    pub codestring: String,
202    /// The version for the ENMPF used in the `codestring`.
203    pub code_version: f32,
204    /// An [EchoPrint](https://github.com/spotify/echoprint-codegen) codestring
205    /// for the track.
206    #[serde(rename = "echoprintstring")]
207    pub echoprint_string: String,
208    /// The version for the EchoPrint format used in the `echoprintstring`.
209    pub echoprint_version: f32,
210    /// A [SynchString](https://github.com/echonest/synchdata) for the track.
211    pub synchstring: String,
212    /// The version for the Synchstring used in the `synchstring`.
213    pub synch_version: f32,
214    /// A Rhythmstring for the track. Its format is similar to that of the
215    /// `synchstring`.
216    pub rhythmstring: String,
217    /// The version for the Rhythmstring used in the `rhythmstring`.
218    pub rhythm_version: f32,
219}
220
221/// A measure (or bar) is a segment of time defined by a specific number of beats.
222#[derive(Clone, Debug, Deserialize, PartialEq)]
223pub struct Bar {
224    /// The starting point, in seconds, of the time interval.
225    pub start: f32,
226    /// The duration, in seconds, of the time interval.
227    pub duration: f32,
228    /// A value ranging betweeen `0.0` - `1.0` that indicates the confidence of
229    /// the interval.
230    pub confidence: f32,
231}
232
233/// A beat is the fundamental time unit of a piece of music; for instance,
234/// each tick of a metronome. Beats are usually multiples of tatums.
235#[derive(Clone, Debug, Deserialize, PartialEq)]
236pub struct Beat {
237    /// The starting point, in seconds, of the time interval.
238    pub start: f32,
239    /// The duration, in seconds, of the time interval.
240    pub duration: f32,
241    /// A value ranging betweeen `0.0` - `1.0` that indicates the confidence of
242    /// the interval.
243    pub confidence: f32,
244}
245
246/// A section is defined by significant changes in rhythm or timbre, such as
247/// the chorus, verse, bridge, guitar solo, etc.
248#[derive(Clone, Debug, Deserialize, PartialEq)]
249pub struct Section {
250    /// The starting point, in seconds, of the section.
251    pub start: f32,
252    /// The duration, in seconds, of the section.
253    pub duration: f32,
254    /// A value ranging betweeen `0.0` - `1.0` that indicates the confidence of
255    /// the section's "designation".
256    pub confidence: f32,
257    /// The average loudness of the section in decibels (dB). Loudness values
258    /// are useful for comparing relative loudness of sections.
259    pub loudness: f32,
260    /// The estimated pace of the section in beats per minute (BPM).
261    pub tempo: f32,
262    /// A value ranging betweeen `0.0` - `1.0` that indicates the confidence of
263    /// the tempo.
264    pub tempo_confidence: f32,
265    /// A value ranging between `-1` - `11` that denotes musical key of the section,
266    /// represented by integers mapping to pitches using standard
267    /// [Pitch Class notation] (https://en.wikipedia.org/wiki/Pitch_class).
268    ///
269    /// If no key is detected, the value is `-1`.
270    pub key: i32,
271    /// A value ranging betweeen `0.0` - `1.0` that indicates the confidence of
272    /// the key.
273    pub key_confidence: f32,
274    /// Indicates the modality (major or minor) of the section.
275    pub mode: Mode,
276    /// A value ranging betweeen `0.0` - `1.0` that indicates the confidence of
277    /// the mode.
278    pub mode_confidence: f32,
279    /// An estimated notation of how many beats are in each measure.
280    ///
281    /// Values range between `3` - `7`, indicating time signatures ranging
282    /// between `3/4` - `7/4`.
283    pub time_signature: u32,
284    /// A value ranging betweeen `0.0` - `1.0` that indicates the confidence of
285    /// the time signature.
286    pub time_signature_confidence: f32,
287}
288
289/// A segment contains a relatively consistent sound throughout its duration.
290#[derive(Clone, Debug, Deserialize, PartialEq)]
291pub struct Segment {
292    /// The starting point, in seconds, of the segment.
293    pub start: f32,
294    /// The duration, in seconds, of the segment.
295    pub duration: f32,
296    /// A value ranging betweeen `0.0` - `1.0` that indicates the confidence of
297    /// the segment.
298    pub confidence: f32,
299    pub loudness_start: f32,
300    pub loudness_max: f32,
301    pub loudness_max_time: f32,
302    pub loudness_end: f32,
303    pub pitches: Vec<f32>,
304    pub timbre: Vec<f32>,
305}
306
307#[derive(Clone, Debug, Deserialize, PartialEq)]
308pub struct Tatum {
309    pub start: f32,
310    pub duration: f32,
311    pub confidence: f32,
312}
313
314#[derive(Clone, Copy, Debug, Deserialize_repr, PartialEq)]
315#[repr(u8)]
316pub enum Mode {
317    Minor,
318    Major,
319}
320
321#[derive(Clone, Copy, Debug, Deserialize_repr, PartialEq)]
322#[repr(u8)]
323pub enum AnalysisStatusCode {
324    Success,
325    Failure,
326}