tomwalters@509: // Copyright 2012, Tom Walters tomwalters@509: // tomwalters@509: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@509: // http://www.acousticscale.org/AIMC tomwalters@509: // tomwalters@509: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@509: // you may not use this file except in compliance with the License. tomwalters@509: // You may obtain a copy of the License at tomwalters@509: // tomwalters@509: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@509: // tomwalters@509: // Unless required by applicable law or agreed to in writing, software tomwalters@509: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@509: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@509: // See the License for the specific language governing permissions and tomwalters@509: // limitations under the License. tomwalters@509: tomwalters@509: /*! tomwalters@509: * \author Tom Walters tomwalters@509: * \date created 2012/02/17 tomwalters@509: * \version \$Id$ tomwalters@509: */ tomwalters@509: tomwalters@509: #include "Modules/Output/OSCOutput.h" tomwalters@509: tomwalters@509: namespace aimc { tomwalters@509: OSCOutput::OSCOutput(Parameters *params) : Module(params) { tomwalters@509: module_description_ = "OSC output"; tomwalters@509: module_identifier_ = "osc_out"; tomwalters@509: module_type_ = "output"; tomwalters@509: module_version_ = "$Id$"; tomwalters@509: tomwalters@509: // Read parameter values from the parameter store. Setting any default tomwalters@509: // values as necessary. The module should set defaults for all parameters tomwalters@509: // that is uses here. The parameters_->DefaultType() methods look for a tomwalters@509: // parameter with a given name. If it already exists in the parameter tomwalters@509: // store, they return the current value. If the parameter doesn't already tomwalters@509: // exist, it is added, set to the default value given, and that value is tomwalters@509: // returned. tomwalters@509: // Examples: tomwalters@509: // integer_param_ = parameters_->DefaultInt("module.param_name", 4); tomwalters@509: // boolean_param_ = parameters_->DefaultBool("module.param_name", true); tomwalters@509: // float_param_ = parameters_->DefaultFloat("module.param_name", 4.4f); tomwalters@509: tomwalters@509: address_ = parameters_->DefaultString("osc.send_address", "127.0.0.1"); tomwalters@509: port_ = parameters_->DefaultInt("osc.send_port", 6448); tomwalters@509: output_buffer_size_ = parameters_->DefaultInt("osc.output_min_buffer_size", 1024); tomwalters@509: } tomwalters@509: tomwalters@509: OSCOutput::~OSCOutput() { tomwalters@509: } tomwalters@509: tomwalters@509: bool OSCOutput::InitializeInternal(const SignalBank &input) { tomwalters@509: // Copy the parameters of the input signal bank into internal variables, so tomwalters@509: // that they can be checked later. tomwalters@509: sample_rate_ = input.sample_rate(); tomwalters@509: buffer_length_ = input.buffer_length(); tomwalters@509: channel_count_ = input.channel_count(); tomwalters@509: tomwalters@509: int output_byte_count = buffer_length_ * channel_count_ * 4 + 2048; // TODO(tom) Find out what the real byte overhead is. tomwalters@509: if (output_buffer_size_ < output_byte_count) { tomwalters@509: output_buffer_size_ = output_byte_count; tomwalters@509: } tomwalters@509: LOG_INFO("Output buffer size is %d", output_buffer_size_); tomwalters@509: LOG_INFO("Feature count is %d", buffer_length_ * channel_count_); tomwalters@509: transmit_socket_ = new UdpTransmitSocket(IpEndpointName(address_.c_str(), port_)); tomwalters@509: buffer_ = new char[output_buffer_size_]; tomwalters@509: packet_stream_ = new osc::OutboundPacketStream(buffer_, output_buffer_size_); tomwalters@509: tomwalters@509: // If this module produces any output, then the output signal bank needs to tomwalters@509: // be initialized here. tomwalters@509: // Example: tomwalters@509: // output_.Initialize(channel_count, buffer_length, sample_rate); tomwalters@509: return true; tomwalters@509: } tomwalters@509: tomwalters@509: void OSCOutput::ResetInternal() { tomwalters@509: // Reset any internal state variables to their default values here. After a tomwalters@509: // call to ResetInternal(), the module should be in the same state as it is tomwalters@509: // just after a call to InitializeInternal(). tomwalters@509: } tomwalters@509: tomwalters@509: void OSCOutput::Process(const SignalBank &input) { tomwalters@509: // Check to see if the module has been initialized. If not, processing tomwalters@509: // should not continue. tomwalters@509: if (!initialized_) { tomwalters@509: LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str()); tomwalters@509: return; tomwalters@509: } tomwalters@509: tomwalters@509: // Check that ths input this time is the same as the input passed to tomwalters@509: // Initialize() tomwalters@509: if (buffer_length_ != input.buffer_length() tomwalters@509: || channel_count_ != input.channel_count()) { tomwalters@509: LOG_ERROR(_T("Mismatch between input to Initialize() and input to " tomwalters@509: "Process() in module %s."), module_identifier_.c_str()); tomwalters@509: return; tomwalters@509: } tomwalters@509: tomwalters@509: // Input is read from the input signal bank using calls like tomwalters@509: // float value = input_.sample(channel_number, sample_index); tomwalters@509: tomwalters@509: (*packet_stream_) << osc::BeginMessage( "/oscCustomFeatures" ); tomwalters@509: for (int ch = 0; ch < input.channel_count(); ch++) { tomwalters@509: for (int i = 0; i < input.buffer_length(); i++) { tomwalters@509: float s = input.sample(ch, i); tomwalters@509: (*packet_stream_) << s; tomwalters@509: } tomwalters@509: } tomwalters@509: (*packet_stream_) << osc::EndMessage; tomwalters@509: transmit_socket_->Send(packet_stream_->Data(), packet_stream_->Size()); tomwalters@509: packet_stream_->Clear(); tomwalters@509: tomwalters@509: // Output is fed into the output signal bank (assuming that it was tomwalters@509: // initialized during the call to InitializeInternal()) like this: tomwalters@509: // output_.set_sample(channel_number, sample_index, sample_value); tomwalters@509: tomwalters@509: // If the output bank is set up, a call to PushOutput() will pass the output tomwalters@509: // on to all the target modules of this module. PushOutput() can be called tomwalters@509: // multiple times within each call to Process(). tomwalters@509: // Example: tomwalters@509: // PushOutput(); tomwalters@509: } tomwalters@509: } // namespace aimc tomwalters@509: