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