1 #include "lib/spotify/audiofeatures.hpp"
2 
items() const3 auto lib::spt::audio_features::items() const -> const std::vector<lib::spt::audio_feature> &
4 {
5 	return values;
6 }
7 
add(::audio_feature feature,float value)8 void lib::spt::audio_features::add(::audio_feature feature, float value)
9 {
10 	values.emplace_back(feature, value);
11 }
12 
add(audio_key key)13 void lib::spt::audio_features::add(audio_key key)
14 {
15 	values.emplace_back(key);
16 }
17 
add(audio_mode mode)18 void lib::spt::audio_features::add(audio_mode mode)
19 {
20 	values.emplace_back(mode);
21 }
22 
to_audio_feature(const std::string & feature)23 auto lib::spt::audio_features::to_audio_feature(const std::string &feature) -> ::audio_feature
24 {
25 	if (feature == "acousticness")
26 	{
27 		return ::audio_feature::acousticness;
28 	}
29 
30 	if (feature == "danceability")
31 	{
32 		return ::audio_feature::danceability;
33 	}
34 
35 	if (feature == "energy")
36 	{
37 		return ::audio_feature::energy;
38 	}
39 
40 	if (feature == "instrumentalness")
41 	{
42 		return ::audio_feature::instrumentalness;
43 	}
44 
45 	if (feature == "key")
46 	{
47 		return ::audio_feature::key;
48 	}
49 
50 	if (feature == "liveness")
51 	{
52 		return ::audio_feature::liveness;
53 	}
54 
55 	if (feature == "loudness")
56 	{
57 		return ::audio_feature::loudness;
58 	}
59 
60 	if (feature == "mode")
61 	{
62 		return ::audio_feature::mode;
63 	}
64 
65 	if (feature == "speechiness")
66 	{
67 		return ::audio_feature::speechiness;
68 	}
69 
70 	if (feature == "tempo")
71 	{
72 		return ::audio_feature::tempo;
73 	}
74 
75 	if (feature == "time_signature")
76 	{
77 		return ::audio_feature::time_signature;
78 	}
79 
80 	if (feature == "valence")
81 	{
82 		return ::audio_feature::valence;
83 	}
84 
85 	return ::audio_feature::unknown;
86 }
87 
from_json(const nlohmann::json & j,audio_features & a)88 void lib::spt::from_json(const nlohmann::json &j, audio_features &a)
89 {
90 	if (!j.is_object())
91 	{
92 		return;
93 	}
94 
95 	for (const auto &item : j.items())
96 	{
97 		const auto feature = lib::spt::audio_features::to_audio_feature(item.key());
98 		if (feature == ::audio_feature::unknown)
99 		{
100 			continue;
101 		}
102 
103 		if (feature == ::audio_feature::key)
104 		{
105 			a.add(item.value().get<audio_key>());
106 			continue;
107 		}
108 
109 		if (feature == ::audio_feature::mode)
110 		{
111 			a.add(item.value().get<audio_mode>());
112 			continue;
113 		}
114 
115 		a.add(feature, item.value().get<float>());
116 	}
117 }
118