Skip to content
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
012902e
[WORKFLOW] Updating protos from viamrobotics/api, commit: e1124aeb085…
viambot Oct 16, 2025
572f247
audio in wrapper
oliviamiller Oct 20, 2025
8a7b980
cleanup
oliviamiller Oct 20, 2025
e98cf28
clean up protos
oliviamiller Oct 20, 2025
ab0bfdf
add example
oliviamiller Oct 20, 2025
b773f7a
clean up
oliviamiller Oct 21, 2025
0d2725d
add get geometries
oliviamiller Oct 21, 2025
fc0869b
run clang format
oliviamiller Oct 21, 2025
75fde70
mock lint
oliviamiller Oct 21, 2025
41b598d
dont use std::byte
oliviamiller Oct 21, 2025
65de978
fix naming conflict with audio_info
oliviamiller Oct 21, 2025
6d495c7
fix equal operator
oliviamiller Oct 21, 2025
716790b
add test cases
oliviamiller Oct 21, 2025
728d566
format
oliviamiller Oct 21, 2025
b997866
pr comments
oliviamiller Oct 21, 2025
7616ba3
lint
oliviamiller Oct 21, 2025
abc6bd5
lint again
oliviamiller Oct 21, 2025
9910fad
properties to audio_properties
oliviamiller Oct 22, 2025
7628375
Merge branch 'main' into audio
lia-viam Oct 22, 2025
ff84ded
include tuple
oliviamiller Oct 22, 2025
2febd1f
Merge branch 'audio' of https://github.com/oliviamiller/viam-cpp-sdk …
oliviamiller Oct 22, 2025
5787c7b
make vars constants
oliviamiller Oct 22, 2025
54ddbac
include tuple in right file
oliviamiller Oct 22, 2025
08e96f6
make request id const
oliviamiller Oct 22, 2025
dd97070
fix M_PI not found on windows
oliviamiller Oct 22, 2025
0c7e883
lint
oliviamiller Oct 22, 2025
faad112
fix typo
oliviamiller Oct 22, 2025
5f66fb2
lint
oliviamiller Oct 22, 2025
deab294
module example
oliviamiller Oct 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions codesamples/apis.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"client": "Arm",
"func": "get_end_position",
"args": []
},
"audio_in": {
"client": "AudioIn",
"func": "get_properties",
"args": []
},
"button": {
"client": "Button",
Expand Down
16 changes: 16 additions & 0 deletions src/viam/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ if (VIAMCPPSDK_USE_DYNAMIC_PROTOS)
${PROTO_GEN_DIR}/component/arm/v1/arm.grpc.pb.h
${PROTO_GEN_DIR}/component/arm/v1/arm.pb.cc
${PROTO_GEN_DIR}/component/arm/v1/arm.pb.h
${PROTO_GEN_DIR}/component/audioin/v1/audioin.grpc.pb.cc
${PROTO_GEN_DIR}/component/audioin/v1/audioin.grpc.pb.h
${PROTO_GEN_DIR}/component/audioin/v1/audioin.pb.cc
${PROTO_GEN_DIR}/component/audioin/v1/audioin.pb.h
${PROTO_GEN_DIR}/component/audioout/v1/audioout.grpc.pb.cc
${PROTO_GEN_DIR}/component/audioout/v1/audioout.grpc.pb.h
${PROTO_GEN_DIR}/component/audioout/v1/audioout.pb.cc
${PROTO_GEN_DIR}/component/audioout/v1/audioout.pb.h
${PROTO_GEN_DIR}/component/base/v1/base.grpc.pb.cc
${PROTO_GEN_DIR}/component/base/v1/base.grpc.pb.h
${PROTO_GEN_DIR}/component/base/v1/base.pb.cc
Expand Down Expand Up @@ -294,6 +302,10 @@ target_sources(viamapi
${PROTO_GEN_DIR}/common/v1/common.pb.cc
${PROTO_GEN_DIR}/component/arm/v1/arm.grpc.pb.cc
${PROTO_GEN_DIR}/component/arm/v1/arm.pb.cc
${PROTO_GEN_DIR}/component/audioin/v1/audioin.grpc.pb.cc
${PROTO_GEN_DIR}/component/audioin/v1/audioin.pb.cc
${PROTO_GEN_DIR}/component/audioout/v1/audioout.grpc.pb.cc
${PROTO_GEN_DIR}/component/audioout/v1/audioout.pb.cc
${PROTO_GEN_DIR}/component/base/v1/base.grpc.pb.cc
${PROTO_GEN_DIR}/component/base/v1/base.pb.cc
${PROTO_GEN_DIR}/component/board/v1/board.grpc.pb.cc
Expand Down Expand Up @@ -358,6 +370,10 @@ target_sources(viamapi
${PROTO_GEN_DIR}/../../viam/api/common/v1/common.pb.h
${PROTO_GEN_DIR}/../../viam/api/component/arm/v1/arm.grpc.pb.h
${PROTO_GEN_DIR}/../../viam/api/component/arm/v1/arm.pb.h
${PROTO_GEN_DIR}/../../viam/api/component/audioin/v1/audioin.grpc.pb.h
${PROTO_GEN_DIR}/../../viam/api/component/audioin/v1/audioin.pb.h
${PROTO_GEN_DIR}/../../viam/api/component/audioout/v1/audioout.grpc.pb.h
${PROTO_GEN_DIR}/../../viam/api/component/audioout/v1/audioout.pb.h
${PROTO_GEN_DIR}/../../viam/api/component/base/v1/base.grpc.pb.h
${PROTO_GEN_DIR}/../../viam/api/component/base/v1/base.pb.h
${PROTO_GEN_DIR}/../../viam/api/component/board/v1/board.grpc.pb.h
Expand Down
1 change: 1 addition & 0 deletions src/viam/examples/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
add_subdirectory(tflite)
add_subdirectory(simple)
add_subdirectory(complex)
add_subdirectory(audioin)
43 changes: 43 additions & 0 deletions src/viam/examples/modules/audioin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2023 Viam Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

add_executable(audioin_module
main.cpp
)

target_link_libraries(audioin_module
PRIVATE Threads::Threads
viam-cpp-sdk::viamsdk
)

install(
TARGETS audioin_module
COMPONENT examples
)

add_executable(audioin_client)
target_sources(audioin_client
PRIVATE
client.cpp
)


target_link_libraries(audioin_client
viam-cpp-sdk::viamsdk
)

install(
TARGETS audioin_client
COMPONENT examples
)
34 changes: 34 additions & 0 deletions src/viam/examples/modules/audioin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# VIAM Simple Module Example
This example goes through how to create a custom audio input resource using Viam's C++ SDK, and how to connect it to a Robot.

This is a limited document. For a more in-depth understanding of modules, see the [documentation](https://docs.viam.com/registry/).

Refer to main.cpp and the comments throughout for more information. For other C++ module examples, refer to the [simple module example](https://github.com/viamrobotics/viam-cpp-sdk/tree/main/src/viam/examples/modules/simple) or [complex module example](https://github.com/viamrobotics/viam-cpp-sdk/tree/main/src/viam/examples/modules/complex).

For a fully fleshed-out example of a C++ module that uses Github CI to upload to the Viam Registry, take a look at [module-example-cpp](https://github.com/viamrobotics/module-example-cpp). For a list of example modules in different Viam SDKs, take a look [here](https://github.com/viamrobotics/upload-module/#example-repos).

This is a limited document. For a more in-depth understanding of modules generally, see the [documentation](https://docs.viam.com/program/extend/modular-resources/).


Example Configuration:
```json{
"components": [
{
"name": "sinewave-audio",
"api": "rdk:component:audio_in",
"model": "viam:audio_in:sinewave",
"attributes": {
"frequency": 440
}
}
],
"modules": [
{
"type": "local",
"name": "my-module",
"executable_path": "/home/viam-cpp-sdk/build/src/viam/examples/modules/audioin/audioin_module"
}
]
}
```

110 changes: 110 additions & 0 deletions src/viam/examples/modules/audioin/client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <iostream>
#include <memory>
#include <string>
#include <vector>

#include <viam/sdk/common/audio.hpp>
#include <viam/sdk/common/instance.hpp>
#include <viam/sdk/common/proto_value.hpp>
#include <viam/sdk/components/audio_in.hpp>
#include <viam/sdk/log/logging.hpp>
#include <viam/sdk/robot/client.hpp>
#include <viam/sdk/rpc/dial.hpp>

using namespace viam::sdk;

int main() {
// Every Viam C++ SDK program must have one and only one Instance object which is created before
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
Instance inst;

std::string host("xarm-main.aqb785vhl4.viam.cloud");
DialOptions dial_opts;
dial_opts.set_entity(std::string("88dcef8e-db7f-47dc-9b0f-eb08fdc5a97d"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't forget to revert these before merging! also in the future it's best to git stash these changes rather than committing them while working on the PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops yeah did not mean to commit this, reverted


Credentials credentials("api-key", "eou798fi90d1fytv66c9lntv1ndwxj4g");

dial_opts.set_credentials(credentials);
boost::optional<DialOptions> opts(dial_opts);
Options options(0, opts);

auto robot = RobotClient::at_address(host, options);

// // Update these with your robot's connection details
// const char* uri = ""; // replace with your robot's URI
// DialOptions dial_options;
// dial_options.set_allow_insecure_downgrade(true); // set to false if connecting securely

// // Uncomment and fill out your credentials details if connecting securely
// // std::string type = "api-key";
// // std::string payload = "your-api-key-here";
// // Credentials credentials(type, payload);
// // dial_options.set_credentials(credentials);

// boost::optional<DialOptions> opts(dial_options);
// std::string address(uri);
// Options options(1, opts);

// std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);

// Print resources
VIAM_SDK_LOG(info) << "Resources:";
std::vector<Name> resource_names = robot->resource_names();
for (const Name& resource : resource_names) {
VIAM_SDK_LOG(info) << " " << resource;
}

// Get the AudioIn component (update with your component name)
auto audio_in = robot->resource_by_name<AudioIn>("sinewave-audio");
if (!audio_in) {
VIAM_SDK_LOG(error) << "could not get 'sinewave-audio' resource from robot";
return EXIT_FAILURE;
}

VIAM_SDK_LOG(info) << "Getting audio properties...";
audio_properties props = audio_in->get_properties();
VIAM_SDK_LOG(info) << "Audio properties:";
VIAM_SDK_LOG(info) << " sample_rate_hz: " << props.sample_rate_hz;
VIAM_SDK_LOG(info) << " num_channels: " << props.num_channels;
VIAM_SDK_LOG(info) << " supported_codecs: " << props.supported_codecs.size() << " codecs";

VIAM_SDK_LOG(info) << "Retrieving 2 seconds of audio...";

std::vector<uint8_t> all_audio_data;
int chunk_count = 0;

// Define chunk handler to collect audio data
auto chunk_handler = [&](AudioIn::audio_chunk&& chunk) -> bool {
chunk_count++;
VIAM_SDK_LOG(info) << "Received chunk " << chunk_count
<< " - length: " << chunk.audio_data.size() << " bytes";

for (const auto& byte : chunk.audio_data) {
all_audio_data.push_back(static_cast<uint8_t>(byte));
}

return true; // Continue receiving chunks
};

// Get 2 seconds of audio (with previous_timestamp = 0 to start from now)
audio_in->get_audio(audio_codecs::PCM_16, chunk_handler, 2.0, 0);

VIAM_SDK_LOG(info) << "Total audio data received: " << all_audio_data.size() << " bytes";
VIAM_SDK_LOG(info) << "Total chunks: " << chunk_count;

std::string filename = "sine_wave_audio.wav";
try {
write_wav_file(filename,
all_audio_data,
audio_codecs::PCM_16,
props.sample_rate_hz,
props.num_channels);
VIAM_SDK_LOG(info) << "Audio saved to " << filename;
VIAM_SDK_LOG(info) << "To play: open " << filename << " (or use any audio player)";
} catch (const std::exception& e) {
VIAM_SDK_LOG(error) << "Failed to write WAV file: " << e.what();
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
Loading