-
Notifications
You must be signed in to change notification settings - Fork 26
RSDK-12150 Add AudioIn Component #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 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 572f247
audio in wrapper
oliviamiller 8a7b980
cleanup
oliviamiller e98cf28
clean up protos
oliviamiller ab0bfdf
add example
oliviamiller b773f7a
clean up
oliviamiller 0d2725d
add get geometries
oliviamiller fc0869b
run clang format
oliviamiller 75fde70
mock lint
oliviamiller 41b598d
dont use std::byte
oliviamiller 65de978
fix naming conflict with audio_info
oliviamiller 6d495c7
fix equal operator
oliviamiller 716790b
add test cases
oliviamiller 728d566
format
oliviamiller b997866
pr comments
oliviamiller 7616ba3
lint
oliviamiller abc6bd5
lint again
oliviamiller 9910fad
properties to audio_properties
oliviamiller 7628375
Merge branch 'main' into audio
lia-viam ff84ded
include tuple
oliviamiller 2febd1f
Merge branch 'audio' of https://github.com/oliviamiller/viam-cpp-sdk …
oliviamiller 5787c7b
make vars constants
oliviamiller 54ddbac
include tuple in right file
oliviamiller 08e96f6
make request id const
oliviamiller dd97070
fix M_PI not found on windows
oliviamiller 0c7e883
lint
oliviamiller faad112
fix typo
oliviamiller 5f66fb2
lint
oliviamiller deab294
module example
oliviamiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,4 @@ | |
| add_subdirectory(tflite) | ||
| add_subdirectory(simple) | ||
| add_subdirectory(complex) | ||
| add_subdirectory(audioin) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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")); | ||
|
|
||
| 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..."; | ||
| 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; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 stashthese changes rather than committing them while working on the PRThere was a problem hiding this comment.
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