Skip to content

Commit 95b2eb3

Browse files
authored
Merge branch 'main' into RSDK-11888-remove-location-secret-connection-code
2 parents 6e1b40f + a856b5a commit 95b2eb3

File tree

21 files changed

+1233
-0
lines changed

21 files changed

+1233
-0
lines changed

codesamples/apis.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
"client": "Arm",
44
"func": "get_end_position",
55
"args": []
6+
},
7+
"audio_in": {
8+
"client": "AudioIn",
9+
"func": "get_properties",
10+
"args": []
611
},
712
"button": {
813
"client": "Button",

src/viam/api/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ if (VIAMCPPSDK_USE_DYNAMIC_PROTOS)
132132
${PROTO_GEN_DIR}/component/arm/v1/arm.grpc.pb.h
133133
${PROTO_GEN_DIR}/component/arm/v1/arm.pb.cc
134134
${PROTO_GEN_DIR}/component/arm/v1/arm.pb.h
135+
${PROTO_GEN_DIR}/component/audioin/v1/audioin.grpc.pb.cc
136+
${PROTO_GEN_DIR}/component/audioin/v1/audioin.grpc.pb.h
137+
${PROTO_GEN_DIR}/component/audioin/v1/audioin.pb.cc
138+
${PROTO_GEN_DIR}/component/audioin/v1/audioin.pb.h
139+
${PROTO_GEN_DIR}/component/audioout/v1/audioout.grpc.pb.cc
140+
${PROTO_GEN_DIR}/component/audioout/v1/audioout.grpc.pb.h
141+
${PROTO_GEN_DIR}/component/audioout/v1/audioout.pb.cc
142+
${PROTO_GEN_DIR}/component/audioout/v1/audioout.pb.h
135143
${PROTO_GEN_DIR}/component/base/v1/base.grpc.pb.cc
136144
${PROTO_GEN_DIR}/component/base/v1/base.grpc.pb.h
137145
${PROTO_GEN_DIR}/component/base/v1/base.pb.cc
@@ -294,6 +302,10 @@ target_sources(viamapi
294302
${PROTO_GEN_DIR}/common/v1/common.pb.cc
295303
${PROTO_GEN_DIR}/component/arm/v1/arm.grpc.pb.cc
296304
${PROTO_GEN_DIR}/component/arm/v1/arm.pb.cc
305+
${PROTO_GEN_DIR}/component/audioin/v1/audioin.grpc.pb.cc
306+
${PROTO_GEN_DIR}/component/audioin/v1/audioin.pb.cc
307+
${PROTO_GEN_DIR}/component/audioout/v1/audioout.grpc.pb.cc
308+
${PROTO_GEN_DIR}/component/audioout/v1/audioout.pb.cc
297309
${PROTO_GEN_DIR}/component/base/v1/base.grpc.pb.cc
298310
${PROTO_GEN_DIR}/component/base/v1/base.pb.cc
299311
${PROTO_GEN_DIR}/component/board/v1/board.grpc.pb.cc
@@ -358,6 +370,10 @@ target_sources(viamapi
358370
${PROTO_GEN_DIR}/../../viam/api/common/v1/common.pb.h
359371
${PROTO_GEN_DIR}/../../viam/api/component/arm/v1/arm.grpc.pb.h
360372
${PROTO_GEN_DIR}/../../viam/api/component/arm/v1/arm.pb.h
373+
${PROTO_GEN_DIR}/../../viam/api/component/audioin/v1/audioin.grpc.pb.h
374+
${PROTO_GEN_DIR}/../../viam/api/component/audioin/v1/audioin.pb.h
375+
${PROTO_GEN_DIR}/../../viam/api/component/audioout/v1/audioout.grpc.pb.h
376+
${PROTO_GEN_DIR}/../../viam/api/component/audioout/v1/audioout.pb.h
361377
${PROTO_GEN_DIR}/../../viam/api/component/base/v1/base.grpc.pb.h
362378
${PROTO_GEN_DIR}/../../viam/api/component/base/v1/base.pb.h
363379
${PROTO_GEN_DIR}/../../viam/api/component/board/v1/board.grpc.pb.h

src/viam/examples/modules/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
add_subdirectory(tflite)
1616
add_subdirectory(simple)
1717
add_subdirectory(complex)
18+
add_subdirectory(audioin)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2023 Viam Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
add_executable(audioin_module
16+
main.cpp
17+
)
18+
19+
target_link_libraries(audioin_module
20+
PRIVATE Threads::Threads
21+
viam-cpp-sdk::viamsdk
22+
)
23+
24+
install(
25+
TARGETS audioin_module
26+
COMPONENT examples
27+
)
28+
29+
add_executable(audioin_client)
30+
target_sources(audioin_client
31+
PRIVATE
32+
client.cpp
33+
)
34+
35+
36+
target_link_libraries(audioin_client
37+
viam-cpp-sdk::viamsdk
38+
)
39+
40+
install(
41+
TARGETS audioin_client
42+
COMPONENT examples
43+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# VIAM Simple Module Example
2+
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.
3+
4+
This is a limited document. For a more in-depth understanding of modules, see the [documentation](https://docs.viam.com/registry/).
5+
6+
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).
7+
8+
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).
9+
10+
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/).
11+
12+
13+
Example Configuration:
14+
```json{
15+
"components": [
16+
{
17+
"name": "sinewave-audio",
18+
"api": "rdk:component:audio_in",
19+
"model": "viam:audio_in:sinewave",
20+
"attributes": {
21+
"frequency": 440
22+
}
23+
}
24+
],
25+
"modules": [
26+
{
27+
"type": "local",
28+
"name": "my-module",
29+
"executable_path": "/home/viam-cpp-sdk/build/src/viam/examples/modules/audioin/audioin_module"
30+
}
31+
]
32+
}
33+
```
34+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)