Mercurial > hg > aimc
comparison trunk/src/Modules/Output/OSCOutput.cc @ 570:4b37b53105a3
Add support for outputting featutes using OSC (for use with the Wekinator, etc).
author | tomwalters@google.com |
---|---|
date | Fri, 22 Jun 2012 12:22:08 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
569:16a590fbc0ba | 570:4b37b53105a3 |
---|---|
1 // Copyright 2012, Tom Walters | |
2 // | |
3 // AIM-C: A C++ implementation of the Auditory Image Model | |
4 // http://www.acousticscale.org/AIMC | |
5 // | |
6 // Licensed under the Apache License, Version 2.0 (the "License"); | |
7 // you may not use this file except in compliance with the License. | |
8 // You may obtain a copy of the License at | |
9 // | |
10 // http://www.apache.org/licenses/LICENSE-2.0 | |
11 // | |
12 // Unless required by applicable law or agreed to in writing, software | |
13 // distributed under the License is distributed on an "AS IS" BASIS, | |
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 // See the License for the specific language governing permissions and | |
16 // limitations under the License. | |
17 | |
18 /*! | |
19 * \author Tom Walters <tom@acousticscale.org> | |
20 * \date created 2012/02/17 | |
21 * \version \$Id$ | |
22 */ | |
23 | |
24 #include "Modules/Output/OSCOutput.h" | |
25 | |
26 namespace aimc { | |
27 OSCOutput::OSCOutput(Parameters *params) : Module(params) { | |
28 module_description_ = "OSC output"; | |
29 module_identifier_ = "osc_out"; | |
30 module_type_ = "output"; | |
31 module_version_ = "$Id$"; | |
32 | |
33 // Read parameter values from the parameter store. Setting any default | |
34 // values as necessary. The module should set defaults for all parameters | |
35 // that is uses here. The parameters_->DefaultType() methods look for a | |
36 // parameter with a given name. If it already exists in the parameter | |
37 // store, they return the current value. If the parameter doesn't already | |
38 // exist, it is added, set to the default value given, and that value is | |
39 // returned. | |
40 // Examples: | |
41 // integer_param_ = parameters_->DefaultInt("module.param_name", 4); | |
42 // boolean_param_ = parameters_->DefaultBool("module.param_name", true); | |
43 // float_param_ = parameters_->DefaultFloat("module.param_name", 4.4f); | |
44 | |
45 address_ = parameters_->DefaultString("osc.send_address", "127.0.0.1"); | |
46 port_ = parameters_->DefaultInt("osc.send_port", 6448); | |
47 output_buffer_size_ = parameters_->DefaultInt("osc.output_min_buffer_size", 1024); | |
48 } | |
49 | |
50 OSCOutput::~OSCOutput() { | |
51 } | |
52 | |
53 bool OSCOutput::InitializeInternal(const SignalBank &input) { | |
54 // Copy the parameters of the input signal bank into internal variables, so | |
55 // that they can be checked later. | |
56 sample_rate_ = input.sample_rate(); | |
57 buffer_length_ = input.buffer_length(); | |
58 channel_count_ = input.channel_count(); | |
59 | |
60 int output_byte_count = buffer_length_ * channel_count_ * 4 + 2048; // TODO(tom) Find out what the real byte overhead is. | |
61 if (output_buffer_size_ < output_byte_count) { | |
62 output_buffer_size_ = output_byte_count; | |
63 } | |
64 LOG_INFO("Output buffer size is %d", output_buffer_size_); | |
65 LOG_INFO("Feature count is %d", buffer_length_ * channel_count_); | |
66 transmit_socket_ = new UdpTransmitSocket(IpEndpointName(address_.c_str(), port_)); | |
67 buffer_ = new char[output_buffer_size_]; | |
68 packet_stream_ = new osc::OutboundPacketStream(buffer_, output_buffer_size_); | |
69 | |
70 // If this module produces any output, then the output signal bank needs to | |
71 // be initialized here. | |
72 // Example: | |
73 // output_.Initialize(channel_count, buffer_length, sample_rate); | |
74 return true; | |
75 } | |
76 | |
77 void OSCOutput::ResetInternal() { | |
78 // Reset any internal state variables to their default values here. After a | |
79 // call to ResetInternal(), the module should be in the same state as it is | |
80 // just after a call to InitializeInternal(). | |
81 } | |
82 | |
83 void OSCOutput::Process(const SignalBank &input) { | |
84 // Check to see if the module has been initialized. If not, processing | |
85 // should not continue. | |
86 if (!initialized_) { | |
87 LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str()); | |
88 return; | |
89 } | |
90 | |
91 // Check that ths input this time is the same as the input passed to | |
92 // Initialize() | |
93 if (buffer_length_ != input.buffer_length() | |
94 || channel_count_ != input.channel_count()) { | |
95 LOG_ERROR(_T("Mismatch between input to Initialize() and input to " | |
96 "Process() in module %s."), module_identifier_.c_str()); | |
97 return; | |
98 } | |
99 | |
100 // Input is read from the input signal bank using calls like | |
101 // float value = input_.sample(channel_number, sample_index); | |
102 | |
103 (*packet_stream_) << osc::BeginMessage( "/oscCustomFeatures" ); | |
104 for (int ch = 0; ch < input.channel_count(); ch++) { | |
105 for (int i = 0; i < input.buffer_length(); i++) { | |
106 float s = input.sample(ch, i); | |
107 (*packet_stream_) << s; | |
108 } | |
109 } | |
110 (*packet_stream_) << osc::EndMessage; | |
111 transmit_socket_->Send(packet_stream_->Data(), packet_stream_->Size()); | |
112 packet_stream_->Clear(); | |
113 | |
114 // Output is fed into the output signal bank (assuming that it was | |
115 // initialized during the call to InitializeInternal()) like this: | |
116 // output_.set_sample(channel_number, sample_index, sample_value); | |
117 | |
118 // If the output bank is set up, a call to PushOutput() will pass the output | |
119 // on to all the target modules of this module. PushOutput() can be called | |
120 // multiple times within each call to Process(). | |
121 // Example: | |
122 // PushOutput(); | |
123 } | |
124 } // namespace aimc | |
125 |