Skip to content

Commit

Permalink
writing to Csound tables from Processing should now be thread safe UN…
Browse files Browse the repository at this point in the history
…TESTED
  • Loading branch information
rorywalsh committed Jan 4, 2013
1 parent 53bc4f9 commit 049eaec
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 47 deletions.
19 changes: 15 additions & 4 deletions csoundo/src/csoundo/CallbackWrapper.java
Expand Up @@ -17,10 +17,21 @@ public CallbackWrapper(Csound csnd){

public int YieldCallback()
{
for(int i=0;i<messageQueue.getNumberOfMessagesInQueue();i++)
//System.out.println("yielded");
csound.SetChannel(messageQueue.getMessageFromQueue(i).channelName, messageQueue.getMessageFromQueue(i).channelData);
//messageQueue.flushMessagesFromQueue();
//update channels
for(int i=0;i<messageQueue.getNumberOfMessagesInChannelQueue();i++)
csound.SetChannel(messageQueue.getMessageFromChannelQueue(i).channelName,
messageQueue.getMessageFromChannelQueue(i).channelData);

//update table values
for(int i=0;i<messageQueue.getNumberOfMessagesInTableQueue();i++)
csound.TableSet(messageQueue.getMessageFromTableQueue(i).tableNumber,
messageQueue.getMessageFromTableQueue(i).xVal,
messageQueue.getMessageFromTableQueue(i).yVal);


//flush messages from queues
messageQueue.flushMessagesFromTableQueue();
messageQueue.flushMessagesFromChannelQueue();
return 1;
}

Expand Down
47 changes: 15 additions & 32 deletions csoundo/src/csoundo/Csoundo.java
Expand Up @@ -342,7 +342,7 @@ public void run() {
*/
public void setChn(String chn, float value) {
if(compiledOK)
callbackWrapper.messageQueue.addMessageToQueue(chn, value);
callbackWrapper.messageQueue.addMessageToChannelQueue(chn, value);
}

/**
Expand All @@ -352,21 +352,6 @@ public void setChn(String chn, String sValue) {
if(compiledOK)
csound.SetChannel(chn, sValue);
}

/* TODO: Try to get this working, even if unnecessary.
public void setChnTest(String chn, float value) {
if (isRunning) {
CsoundMYFLTArray myflt = new CsoundMYFLTArray(4);
int check = csnd.csoundGetChannelPtr(csound_p, myflt.GetPtr(), chn,
csnd.CSOUND_CONTROL_CHANNEL |
csnd.CSOUND_INPUT_CHANNEL);
if (check == 0) {
myflt.SetValue(0, value);
}
}
}
*/

/**
* Overwrites the Csound options.
Expand Down Expand Up @@ -398,35 +383,33 @@ public float sr() {
* @param i Index
* @return Csound table value
*/

public float tableGet(int t, int i) {
// TODO: If sets are locked, do gets need to be?
if (!compiledOK) return 0;
// mutex.lock();
return (float) csound.TableGet(t, i);
// mutex.unlock();
}

/**
* Return the length of a Csound table.
*
* @param t Table number
* @return Csound table length
*/
/**
* Return the length of a Csound table.
*
* @param t Table number
* @return Csound table length
*/
public int tableLength(int t) {
if(!compiledOK) return -99999;
return csound.TableLength(t);
}

/**
* Sets the value of a Csound table at a specif index.
*
* @param t Table number
* @param i Index
* @param v Value
*/
* Sets the value of a Csound table at a specif index.
*
* @param t Table number
* @param i Index
* @param v Value
*/
public void tableSet(int t, int i, float v) {
if(compiledOK)
csound.TableSet(t, i, v);
callbackWrapper.messageQueue.addMessageToTableQueue(t, i, v);
}

}
40 changes: 29 additions & 11 deletions csoundo/src/csoundo/MessageQueue.java
Expand Up @@ -35,27 +35,45 @@
*/
public class MessageQueue {
private String csd;
private Vector<ChannelMessage> messages;
private Vector<ChannelMessage> channelMessageQueue;
private Vector<TableMessage> tableMessageQueue;


public MessageQueue(){
messages = new Vector<ChannelMessage>();
channelMessageQueue = new Vector<ChannelMessage>();
tableMessageQueue = new Vector<TableMessage>();
}

public void addMessageToQueue(String _chan, double _val){
messages.addElement(new ChannelMessage(_chan, _val));
public void addMessageToChannelQueue(String _chan, double _val){
channelMessageQueue.addElement(new ChannelMessage(_chan, _val));
}

public ChannelMessage getMessageFromQueue(int index){
return messages.get(index);
public void addMessageToTableQueue(int _tableNumber, int _index, double _amp){
tableMessageQueue.addElement(new TableMessage(_tableNumber, _index, _amp));
}

public int getNumberOfMessagesInQueue(){
return messages.size();
public ChannelMessage getMessageFromChannelQueue(int index){
return channelMessageQueue.get(index);
}

public void flushMessagesFromQueue(){
messages.removeAllElements();

public TableMessage getMessageFromTableQueue(int index){
return tableMessageQueue.get(index);
}

public int getNumberOfMessagesInChannelQueue(){
return channelMessageQueue.size();
}

public int getNumberOfMessagesInTableQueue(){
return tableMessageQueue.size();
}

public void flushMessagesFromChannelQueue(){
channelMessageQueue.removeAllElements();
}

public void flushMessagesFromTableQueue(){
tableMessageQueue.removeAllElements();
}
}

46 changes: 46 additions & 0 deletions csoundo/src/csoundo/TableMessage.java
@@ -0,0 +1,46 @@
/**
* A Csound interface library for Processing.
*
* (c) 2010
*
* This library 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 library 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 library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* @author Jacob Joaquin, Rory Walsh, Conor Dempsey
* @modified 10/01/2012
* @version 0.2.1
*/

package csoundo;

import csnd.*;
import java.io.*;
import java.util.*;

/**
* Simple class to hold channel name and value.
*/

public class TableMessage {
public int xVal;
public double yVal;
public int tableNumber;

public TableMessage(int tableNum, int xPos, double yPos){
xVal = xPos;
yVal = yPos;
tableNumber = tableNum;
}
}

0 comments on commit 049eaec

Please sign in to comment.