Official C++ SDK for music recognition API: identify music from a short audio clip, a long audio file, or a live stream.
The API itself is so simple that it can easily be used even without an SDK: docs.audd.io.
Requires a C++17 compiler and libcurl (Ubuntu: apt install libcurl4-openssl-dev; macOS: brew install curl). Add the SDK to your CMake project:
# CMakeLists.txt include(FetchContent) FetchContent_Declare(audd GIT_REPOSITORY https://github.com/AudDMusic/audd-cpp.git GIT_TAG v1.5.18 ) FetchContent_MakeAvailable(audd) target_link_libraries(your_app PRIVATE audd::audd)
To install it once for every project instead:
git clone --branch v1.5.18 https://github.com/AudDMusic/audd-cpp.git cmake -S audd-cpp -B audd-cpp/build cmake --build audd-cpp/build sudo cmake --install audd-cpp/build
Then use find_package(audd CONFIG REQUIRED) in place of the FetchContent lines.
Get your API token at dashboard.audd.io.
Identify a song hosted at a URL:
#include <iostream> #include <audd/audd.hpp> int main() { audd::AudD client("your-api-token"); // get a real token at dashboard.audd.io try { auto result = client.recognize("https://audd.tech/example.mp3"); if (!result) { std::cout << "no match\n"; return 0; } std::cout << result->artist << " - " << result->title << "\n"; } catch (const audd::AudDError& e) { std::cerr << e.what() << "\n"; // e.g. "[#900] ..." for an invalid token return 1; } }
Identify a song from a local file path:
auto result = client.recognize(audd::SourceFilePath{"/path/to/clip.mp3"});
recognize() accepts a std::string (auto-classified as URL or file path), an explicit audd::SourceUrl / audd::SourceFilePath, or an audd::SourceBytes carrying raw audio bytes. For longer audio files, use recognize_enterprise(source, opts), which returns std::vector<EnterpriseMatch> across the file's chunks. Each match carries the same core tags plus score, isrc, upc, and start_seconds / end_seconds — where the match plays in your file, in seconds. These are precise because the SDK requests accurate offsets by default; pass opts.accurate_offsets = false to turn that off. Behind them, start_offset / end_offset are the raw fragment-relative milliseconds. Access to isrc, upc, and score requires a Startup plan or higher — contact us for enterprise features. A match from your custom catalog carries audio_id instead of artist/title; check m.is_custom_match() and read *m.audio_id.
Every blocking method has an _async twin returning std::future: recognize_async, recognize_enterprise_async, streams().add_async, etc. Reach for the future-based form when you want non-blocking dispatch from a UI thread or want to fan out concurrent calls. Keep the AudD client alive until every future has completed — the async work borrows the client. The same applies to streams().longpoll(...): keep the client alive until the poll is stopped.
The library is single-target (audd::audd); link it and #include <audd/audd.hpp>.
Pass your token to the constructor:
audd::AudD client("your-api-token");
Or leave it empty and the SDK reads the AUDD_API_TOKEN environment variable:
// AUDD_API_TOKEN=your-token ... audd::AudD client("");
Get a real token at dashboard.audd.io. The public "test" token works for a quick try without signing up, capped at 10 requests/day.
For long-running services that pull tokens from a secret manager and need to swap them without restarting:
client.set_api_token(new_token);
In-flight requests continue with the previous token; subsequent ones use the new value.
If you'd rather fail fast at construction time when no token is configured, use audd::AudD::strict(""), which throws audd::AudDMissingApiTokenError.
By default recognize() returns the core tags plus AudD's universal song link — no metadata-block opt-in needed:
auto result = client.recognize("https://audd.tech/example.mp3"); if (!result) return; // no match std::cout << result->artist << " - " << result->title << "\n"; std::cout << "Album: " << result->album << "\n"; std::cout << "Released: " << result->release_date << "\n"; std::cout << "Label: " << result->label << "\n"; std::cout << "AudD song: " << result->song_link << "\n"; // links into every provider // Helpers driven off song_link — work without any return-metadata opt-in: std::cout << "Cover art: " << result->thumbnail_url() << "\n"; std::cout << "On Spotify: " << result->streaming_url(audd::StreamingProvider::Spotify) << "\n"; for (const auto& [provider, url] : result->streaming_urls()) { std::cout << " " << audd::streaming_provider_name(provider) << " -> " << url << "\n"; }
If you need provider-specific metadata blocks, opt in per call. Request only what you need — each provider you ask for adds latency:
audd::RecognizeOptions opts;
opts.return_metadata = {"apple_music", "spotify"};
auto result = client.recognize("https://audd.tech/example.mp3", opts);
if (result->apple_music) std::cout << "Apple Music: " << result->apple_music->url << "\n";
if (result->spotify) std::cout << "Spotify URI: " << result->spotify->uri << "\n";
std::cout << "Preview: " << result->preview_url() << "\n"; // first across requested providers; "" if noneValid return_metadata values: apple_music, spotify, deezer, musicbrainz. The metadata-block fields are std::optional<...>; musicbrainz is a std::vector<...>.
result->extras is a std::map<std::string, nlohmann::json> of every server field outside the typed surface; result->raw_response is the original JSON string. Use them to read additional fields the SDK doesn't surface as typed properties:
if (auto it = result->extras.find("song_length"); it != result->extras.end()) { int seconds = it->second.get<int>(); std::cout << "song length: " << seconds << "\n"; } // Same channel exists on every metadata block: if (result->apple_music) { if (auto it = result->apple_music->extras.find("genreNames"); it != result->apple_music->extras.end()) { auto genres = it->second.get<std::vector<std::string>>(); } }
For sending arbitrary form fields the typed parameters don't cover, set the extra_parameters map on the options struct:
audd::RecognizeOptions opts;
opts.return_metadata = {"apple_music"};
opts.extra_parameters = {{"my_custom_flag", "1"}};
auto result = client.recognize(source, opts);Typed parameters win on collision.
Every error from the API or the network derives from audd::AudDError. A mistake in the arguments themselves throws std::invalid_argument: a recognize() string that is neither an http(s):// URL nor an existing file, or an empty token passed to set_api_token(). Discriminate AudDError by category:
try { auto result = client.recognize(source); // ... } catch (const audd::AudDError& e) { switch (e.category()) { case audd::ErrorCategory::Authentication: // 900 / 901 / 903 — token problems break; case audd::ErrorCategory::Quota: // 902 — quota exceeded break; case audd::ErrorCategory::InvalidAudio: // 300 / 400 / 500 — audio is the problem break; case audd::ErrorCategory::RateLimit: // 611 — back off and retry break; case audd::ErrorCategory::Server: // 5xx, non-JSON gateway responses break; default: std::cerr << "audd: " << e.what() << "\n"; } }
For the underlying numeric code, message, and request ID, catch the typed error:
try { ... } catch (const audd::AudDApiError& e) { std::cerr << "[#" << e.error_code << "] " << e.message << " (request_id=" << e.request_id << ")\n"; }
Categories: Authentication, Quota, Subscription, CustomCatalogAccess, InvalidRequest, InvalidAudio, RateLimit, StreamLimit, NotReleased, Blocked, NeedsUpdate, Server, Connection, Serialization.
audd::ClientConfig cfg; cfg.max_attempts = 5; // default 3 cfg.backoff_factor = std::chrono::seconds(1); // default 500 ms cfg.standard_timeout = std::chrono::minutes(2); // default 60 s cfg.enterprise_timeout = std::chrono::hours(2); // default 1 h cfg.on_event = [](const audd::AudDEvent& e) { // see Observability }; cfg.on_deprecation = [](const std::string& msg) { // server-side soft-deprecation warnings (code 51) land here std::cerr << "audd-deprecation: " << msg << "\n"; }; audd::AudD client("your-api-token", cfg);
Retries are conservative: connection-level failures (DNS, TCP, TLS) are retried up to max_attempts with exponential backoff. Post-upload errors are not retried — the server may have already done the metered work.
cfg.on_event receives a structured AudDEvent for every Request / Response / Exception on the wire — method/URL/status/elapsed/request_id, no api_token, no body bytes:
cfg.on_event = [](const audd::AudDEvent& e) { std::cout << "audd " << (e.kind == audd::AudDEvent::Kind::Request ? "req" : e.kind == audd::AudDEvent::Kind::Response ? "rsp" : "exc") << " " << e.method << " " << e.http_status << " " << e.elapsed.count() << "ms " << "req_id=" << e.request_id << "\n"; };
Hook errors are swallowed by the SDK — observability never breaks the request path.
Stream recognition turns AudD into a continuous monitor for an audio stream (internet radio, Twitch, YouTube live, raw HLS/Icecast) and notifies you for every recognized song. Set up streams once, then either receive matches via a callback URL or poll for them.
auto& streams = client.streams(); // 1. Tell AudD where to POST recognition results for your account. audd::SetCallbackUrlOptions opts; opts.return_metadata = {"apple_music", "spotify"}; streams.set_callback_url("https://your.app/audd/callback", opts); // 2. Add streams to monitor. radio_id is any integer you choose. audd::AddStreamRequest stream; stream.url = "https://example.com/radio.m3u8"; stream.radio_id = 1; streams.add(stream); stream.url = "twitch:somechannel"; stream.radio_id = 2; streams.add(stream); // 3. Inspect what you have configured. for (const auto& s : streams.list()) { std::cout << s.radio_id << " " << s.url << " running=" << s.stream_running << "\n"; }
Inside your callback receiver, parse the POST body into a typed event:
auto event = audd::handle_callback(request_body); std::visit([](auto&& v) { using T = std::decay_t<decltype(v)>; if constexpr (std::is_same_v<T, audd::StreamCallbackMatch>) { std::cout << "matched: " << v.song.artist << " - " << v.song.title << "\n"; for (const auto& alt : v.alternatives) { std::cout << " alt: " << alt.artist << " - " << alt.title << "\n"; } } else if constexpr (std::is_same_v<T, audd::StreamCallbackNotification>) { std::cout << "notification: " << v.notification_message << "\n"; } }, event);
handle_callback(body) and parse_callback(body) accept either std::string or raw bytes. The result is a std::variant<StreamCallbackMatch, StreamCallbackNotification>; every event reaches the right if constexpr branch.
See examples/streams_callback_handler.cpp (using cpp-httplib) and examples/streams_setup.cpp for runnable code.
If hosting a callback receiver isn't an option, longpoll for events from the client side. The poll exposes three asynchronous streams — matches, notifications, and a single terminal error — consumable in three ways.
Callback-style (blocks the calling thread until terminal):
int radio_id = 1; // any integer you choose — your handle for this stream auto poll = client.streams().longpoll(radio_id); poll.run( [](audd::StreamCallbackMatch m) { std::cout << "matched: " << m.song.artist << " - " << m.song.title << "\n"; }, [](audd::StreamCallbackNotification n) { std::cout << "notification: " << n.notification_message << "\n"; }, [](std::exception_ptr ep) { try { std::rethrow_exception(ep); } catch (const std::exception& e) { std::cerr << "longpoll: " << e.what() << "\n"; } });
run() calls the callbacks on the thread that called it, one at a time, in the order the events arrived; an exception thrown from one propagates out of run().
Blocking pull (one event at a time, your own loop):
auto poll = client.streams().longpoll(category); while (auto m = poll.next_match()) { std::cout << m->song.artist << " - " << m->song.title << "\n"; }
Future-based (non-blocking dispatch):
auto fut = poll.next_match_async(); // ... do other work ... auto m = fut.get(); // std::optional<StreamCallbackMatch>; nullopt = stream closed
Each request asks only for events newer than the newest one already received. The poll keeps going through network failures and HTTP 5xx, waiting before the next try (1 second at first, doubling up to 30); pass on_transient_error to see those failures:
audd::LongpollOptions opts;
opts.on_transient_error = [](std::exception_ptr ep) { // runs on the poll's own thread
try { std::rethrow_exception(ep); }
catch (const std::exception& e) { std::cerr << "longpoll retrying: " << e.what() << "\n"; }
};
auto poll = client.streams().longpoll(radio_id, opts);The terminal error is reserved for requests the server rejects: for example, an opts.timeout_seconds outside 1–120 (it sets how long the server holds each request open; default 50) ends the poll with an audd::AudDInvalidRequestError carrying the server's message. poll.close(), or the poll going out of scope, stops it within about a second, even while a request is waiting.
audd::derive_longpoll_category(token, radio_id) is also available as a free function — useful for computing categories on a server and shipping them to a frontend without leaking the api_token. The longpoll requests themselves never carry the token, so a client constructed without one can poll such a category: audd::AudD consumer(""); auto poll = consumer.streams().longpoll(category);.
Warning
The custom-catalog endpoint is not music recognition. It adds songs to your account's private fingerprint database, so AudD's recognition can later identify your own tracks for your account only. If you intended to identify music, use client.recognize(...) (or client.recognize_enterprise(...) for longer audio files) instead.
client.custom_catalog().add(audio_id, audd::SourceFilePath{"/path/to/track.mp3"});add() is sent once and never retried automatically: check the error before sending it again (an upload to an audio_id that already holds a song replaces it).
Custom-catalog access requires a separate subscription. Contact [email protected] to get it enabled. Subscription failures throw audd::AudDCustomCatalogAccessError, which carries a long, friendlier message than the generic Subscription category.
client.advanced() exposes a generic raw-method escape hatch for AudD endpoints not yet wrapped on this SDK:
auto body = client.advanced().raw_request("newMethodName", {{"param", "value"}}); // body is a nlohmann::json
MIT — see LICENSE. Security policy: SECURITY.md.
Bug reports and PRs welcome. For account / API questions, email [email protected].
This SDK vendors three single-header libraries to keep build setup minimal:
vendor/nlohmann/json.hpp— nlohmann/json, MITvendor/cpp-httplib/httplib.h— yhirose/cpp-httplib, MIT (used by examples)vendor/doctest/doctest.h— doctest/doctest, MIT (used by tests)
libcurl is the one external dependency (used outbound).
The public headers #include <nlohmann/json.hpp>. install ships the bundled copy under include/audd/third_party, out of the way of any nlohmann/json in the global include directory, and puts it on the exported target's include path, so find_package(audd) needs nothing else and your code compiles against the same nlohmann/json (3.11.3) the library was built with. To provide your own copy of that version instead, configure with -DAUDD_INSTALL_VENDORED_JSON=OFF.