|
| 1 | +#include <iostream> |
| 2 | +#include <memory> |
| 3 | +#include <string> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include <viam/sdk/common/audio.hpp> |
| 7 | +#include <viam/sdk/common/instance.hpp> |
| 8 | +#include <viam/sdk/common/proto_value.hpp> |
| 9 | +#include <viam/sdk/components/audio_in.hpp> |
| 10 | +#include <viam/sdk/log/logging.hpp> |
| 11 | +#include <viam/sdk/robot/client.hpp> |
| 12 | +#include <viam/sdk/rpc/dial.hpp> |
| 13 | + |
| 14 | +using namespace viam::sdk; |
| 15 | + |
| 16 | +int main() { |
| 17 | + // Every Viam C++ SDK program must have one and only one Instance object which is created before |
| 18 | + // any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed. |
| 19 | + Instance inst; |
| 20 | + const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely |
| 21 | + DialOptions dial_options; |
| 22 | + dial_options.set_allow_insecure_downgrade(true); // set to false if connecting securely |
| 23 | + |
| 24 | + // Uncomment and fill out your credentials details if connecting securely |
| 25 | + // std::string type = "api-key"; |
| 26 | + // std::string payload = "your-api-key-here"; |
| 27 | + // Credentials credentials(type, payload); |
| 28 | + // dial_options.set_credentials(credentials); |
| 29 | + |
| 30 | + boost::optional<DialOptions> opts(dial_options); |
| 31 | + std::string address(uri); |
| 32 | + Options options(1, opts); |
| 33 | + |
| 34 | + std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options); |
| 35 | + |
| 36 | + // Print resources |
| 37 | + VIAM_SDK_LOG(info) << "Resources:"; |
| 38 | + std::vector<Name> resource_names = robot->resource_names(); |
| 39 | + for (const Name& resource : resource_names) { |
| 40 | + VIAM_SDK_LOG(info) << " " << resource; |
| 41 | + } |
| 42 | + |
| 43 | + // Get the AudioIn component (update with your component name) |
| 44 | + auto audio_in = robot->resource_by_name<AudioIn>("sinewave-audio"); |
| 45 | + if (!audio_in) { |
| 46 | + VIAM_SDK_LOG(error) << "could not get 'sinewave-audio' resource from robot"; |
| 47 | + return EXIT_FAILURE; |
| 48 | + } |
| 49 | + |
| 50 | + VIAM_SDK_LOG(info) << "Getting audio properties..."; |
| 51 | + audio_properties props = audio_in->get_properties(); |
| 52 | + VIAM_SDK_LOG(info) << "Audio properties:"; |
| 53 | + VIAM_SDK_LOG(info) << " sample_rate_hz: " << props.sample_rate_hz; |
| 54 | + VIAM_SDK_LOG(info) << " num_channels: " << props.num_channels; |
| 55 | + VIAM_SDK_LOG(info) << " supported_codecs: " << props.supported_codecs.size() << " codecs"; |
| 56 | + |
| 57 | + VIAM_SDK_LOG(info) << "Retrieving 2 seconds of audio..."; |
| 58 | + |
| 59 | + std::vector<uint8_t> all_audio_data; |
| 60 | + int chunk_count = 0; |
| 61 | + |
| 62 | + // Define chunk handler to collect audio data |
| 63 | + auto chunk_handler = [&](AudioIn::audio_chunk&& chunk) -> bool { |
| 64 | + chunk_count++; |
| 65 | + VIAM_SDK_LOG(info) << "Received chunk " << chunk_count |
| 66 | + << " - length: " << chunk.audio_data.size() << " bytes"; |
| 67 | + |
| 68 | + for (const auto& byte : chunk.audio_data) { |
| 69 | + all_audio_data.push_back(static_cast<uint8_t>(byte)); |
| 70 | + } |
| 71 | + |
| 72 | + return true; // Continue receiving chunks |
| 73 | + }; |
| 74 | + |
| 75 | + // Get 2 seconds of audio (with previous_timestamp = 0 to start from now) |
| 76 | + audio_in->get_audio(audio_codecs::PCM_16, chunk_handler, 2.0, 0); |
| 77 | + |
| 78 | + VIAM_SDK_LOG(info) << "Total audio data received: " << all_audio_data.size() << " bytes"; |
| 79 | + VIAM_SDK_LOG(info) << "Total chunks: " << chunk_count; |
| 80 | + |
| 81 | + std::string filename = "sine_wave_audio.wav"; |
| 82 | + try { |
| 83 | + write_wav_file(filename, |
| 84 | + all_audio_data, |
| 85 | + audio_codecs::PCM_16, |
| 86 | + props.sample_rate_hz, |
| 87 | + props.num_channels); |
| 88 | + VIAM_SDK_LOG(info) << "Audio saved to " << filename; |
| 89 | + VIAM_SDK_LOG(info) << "To play: open " << filename << " (or use any audio player)"; |
| 90 | + } catch (const std::exception& e) { |
| 91 | + VIAM_SDK_LOG(error) << "Failed to write WAV file: " << e.what(); |
| 92 | + return EXIT_FAILURE; |
| 93 | + } |
| 94 | + |
| 95 | + return EXIT_SUCCESS; |
| 96 | +} |
0 commit comments