<commit_before>/*
    csound~ : A MaxMSP external interface for the Csound API.
    
    Created by <NAME> on 2/4/06.
    Copyright 2006-2010 <NAME>. All rights reserved.
    
    LICENSE AGREEMENT
    
    This software is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    
    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this software; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "channel.h"

using namespace std;
using namespace dvx;
extern int MaxChannelStringLength;

ChannelObject::ChannelObject(CSOUND* csound, const char *name, int type) :
  m_csound(csound),
	m_name(name), 
	m_type(type), 
	m_value(FL(0.0)), 
	m_str_value(),
	m_dirty(true),
	m_csoundChanPtr(NULL), 
	m_sym(gensym((char*)m_name.c_str()))
{
}

bool ChannelObject::IsInputChannel() const { return m_type & CSOUND_INPUT_CHANNEL; }
bool ChannelObject::IsOutputChannel() const { return m_type & CSOUND_INPUT_CHANNEL; }
bool ChannelObject::IsControlChannel() const
{
	return (m_type & CSOUND_CHANNEL_TYPE_MASK) == CSOUND_CONTROL_CHANNEL;
}
bool ChannelObject::IsStringChannel() const
{
	return (m_type & CSOUND_CHANNEL_TYPE_MASK) == CSOUND_STRING_CHANNEL;
}

void ChannelObject::SetVal(MYFLT val)
{
	m_value = val;
	if(m_csoundChanPtr)
		*m_csoundChanPtr = m_value;
	m_dirty = true;
}

void ChannelObject::SetStringVal(const char *str)
{	
	assert(str);
	m_str_value = str;
	if(m_csoundChanPtr)
    csoundSetStringChannel(m_csound, m_name.c_str(), (char*)str);
	m_dirty = true;
}

void ChannelObject::GetStringVal()
{
  if(m_csoundChanPtr) {
    int strsize = csoundGetChannelDatasize(m_csound, m_name.c_str());
    char* data = (char*)malloc(strsize + 1);
    csoundGetStringChannel(m_csound, m_name.c_str(), data);
    m_str_value = data;
    free(data);
  }
}

void ChannelObject::SyncVal(Direction d)
{
	if(!m_csoundChanPtr) return;
	if(IN_ == d)
	{
		if(IsControlChannel())
			*m_csoundChanPtr = m_value;
		else if(IsStringChannel())
			strncpy((char*)m_csoundChanPtr, m_str_value.c_str(), MaxChannelStringLength-1);
	}
	else if(OUT_ == d)
	{
		if(IsControlChannel())
			m_value = *m_csoundChanPtr;
		else if(IsStringChannel())
			m_str_value = (char*)m_csoundChanPtr;<commit_msg>fixed issue with string channel initialization in csound~<commit_after>/*
    csound~ : A MaxMSP external interface for the Csound API.
    
    Created by <NAME> on 2/4/06.
    Copyright 2006-2010 <NAME>. All rights reserved.
    
    LICENSE AGREEMENT
    
    This software is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    
    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this software; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "channel.h"

using namespace std;
using namespace dvx;
extern int MaxChannelStringLength;

ChannelObject::ChannelObject(CSOUND* csound, const char *name, int type) :
  m_csound(csound),
	m_name(name), 
	m_type(type), 
	m_value(FL(0.0)), 
	m_str_value(),
	m_dirty(true),
	m_csoundChanPtr(NULL), 
	m_sym(gensym((char*)m_name.c_str()))
{
}

bool ChannelObject::IsInputChannel() const { return m_type & CSOUND_INPUT_CHANNEL; }
bool ChannelObject::IsOutputChannel() const { return m_type & CSOUND_INPUT_CHANNEL; }
bool ChannelObject::IsControlChannel() const
{
	return (m_type & CSOUND_CHANNEL_TYPE_MASK) == CSOUND_CONTROL_CHANNEL;
}
bool ChannelObject::IsStringChannel() const
{
	return (m_type & CSOUND_CHANNEL_TYPE_MASK) == CSOUND_STRING_CHANNEL;
}

void ChannelObject::SetVal(MYFLT val)
{
	m_value = val;
	if(m_csoundChanPtr)
		*m_csoundChanPtr = m_value;
	m_dirty = true;
}

void ChannelObject::SetStringVal(const char *str)
{	
	assert(str);
	m_str_value = str;
	if(m_csoundChanPtr)
    csoundSetStringChannel(m_csound, m_name.c_str(), (char*)str);
	m_dirty = true;
}

void ChannelObject::GetStringVal()
{
  if(m_csoundChanPtr) {
    int strsize = csoundGetChannelDatasize(m_csound, m_name.c_str());
    char* data = (char*)malloc(strsize + 1);
    csoundGetStringChannel(m_csound, m_name.c_str(), data);
    m_str_value = data;
    free(data);
  }
}

void ChannelObject::SyncVal(Direction d)
{
	if(!m_csoundChanPtr) return;
	if(IN_ == d)
	{
		if(IsControlChannel())
			*m_csoundChanPtr = m_value;
		else if(IsStringChannel())
      SetStringVal(m_str_value.c_str());
	}
	else if(OUT_ == d)
	{
		if(IsControlChannel())
			m_value = *m_csoundChanPtr;
		else if(IsStringChannel())
      GetStringVal();
{'fraction_non_alphanumeric': 0.09892119217407204, 'fraction_numerical': 0.01151947339550192, 'mean_word_length': 4.431976166832174, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4217763', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1832, 'n_words': 515}