Skip to content

Commit

Permalink
committing some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
blucia0a committed Mar 24, 2012
1 parent 8873aca commit 3f25552
Showing 1 changed file with 36 additions and 179 deletions.
215 changes: 36 additions & 179 deletions Streamy.c
Expand Up @@ -12,128 +12,16 @@ int NUM_SYNTHS;
#define NUM_PORTS 6
int g_level;
int doMouse = 0;
AUNode *SynthNodes;
AudioUnit *SynthUnits;
AudioUnit OutputUnit;

AUGraph AudioGraph;
enum {
kMIDINoteOff = 128,
kMIDINoteOn = 144,
kMIDICtrl = 176,
kMIDIPrgm = 192
};

int voice = 0;
enum {
kKeyboardAKey = 0x00,
kKeyboardBKey = 0x0b,
kKeyboardCKey = 0x08,
kKeyboardDKey = 0x02,
kKeyboardEKey = 0x0e,
kKeyboardFKey = 0x03,
kKeyboardGKey = 0x05,
kKeyboardHKey = 0x04,
kKeyboardIKey = 0x22,
kKeyboardJKey = 0x26,
kKeyboardKKey = 0x28,
kKeyboardLKey = 0x25,
kKeyboardMKey = 0x2e,
kKeyboardNKey = 0x2d,
kKeyboardOKey = 0x1f,
kKeyboardPKey = 0x23,
kKeyboardQKey = 0x0c,
kKeyboardRKey = 0x0f,
kKeyboardSKey = 0x01,
kKeyboardTKey = 0x11,
kKeyboardUKey = 0x20,
kKeyboardVKey = 0x09,
kKeyboardWKey = 0x0d,
kKeyboardXKey = 0x07,
kKeyboardYKey = 0x10,
kKeyboardZKey = 0x06,

kKeyboardEscapeKey = 0x35,
kKeyboardSpaceKey = 0x31,
kKeyboardCommaKey = 0x2b,
kKeyboardStopKey = 0x2f,
kKeyboardSlashKey = 0x2c,
kKeyboardColonKey = 0x29,
kKeyboardQuoteKey = 0x27,
kKeyboardLBrktKey = 0x21
};


typedef struct _key{
bool down;
int tone;/*0x3c is middle c*/
} key;

key Keys[13];
CGEventRef keyPressed(
CGEventTapProxy proxy,
CGEventType type,
CGEventRef event,
void *refcon
){
AudioUnit *SUs = (AudioUnit *)refcon;

if((type == kCGEventMouseMoved)){
CGPoint cursor = CGEventGetLocation(event);
float my = cursor.y;
float frac = my/900.;
int newval = (int)((float)65535 * frac);

//fprintf(stderr,"%d\n",newval);

if( doMouse ){
for(int i = 0; i < NUM_SYNTHS; i++){
MusicDeviceMIDIEvent(SUs[i], 224, (newval & 0xFF), ( newval & 0xFF00 ) >> 8, 0);
}
}
return event;

}

if ((type != kCGEventKeyDown) && (type != kCGEventKeyUp)){
return event;
}

// The incoming keycode.
CGKeyCode keycode = (CGKeyCode)CGEventGetIntegerValueField(
event, kCGKeyboardEventKeycode);

if( keycode == kKeyboardQKey && type == kCGEventKeyDown ){

exit(0);

}


return event;

}

typedef struct _playParams{

int id;
unsigned long period;
unsigned long duration;
unsigned tone;

} playParams;

void * periodic_play(void *p){

playParams *pps = (playParams*)p;
while(1){
MusicDeviceMIDIEvent(SynthUnits[pps->id], kMIDINoteOn, pps->tone, 127, 0);
usleep( pps->duration );
MusicDeviceMIDIEvent(SynthUnits[pps->id], kMIDINoteOff, pps->tone, 127, 0);
usleep( pps->period);
}

}
int voice = 0;

typedef struct _cmd{
int hostNum;
Expand Down Expand Up @@ -196,30 +84,28 @@ void * sequence_play(void *p){
for(int i = 0; i < pps->numSegments; i++){

int tone = pps->tone[i] * (1 + curCmd.dir);
MIDIPacketList packetList;
packetList.numPackets = 1;
packetList.packet[0].length = 3;
packetList.packet[0].data[0] = 0x90; // note ON
//packetList.packet[0].data[1] = 60 & 0x7F; // C
packetList.packet[0].data[1] = tone; // C
packetList.packet[0].data[2] = level; // velocity
packetList.packet[0].timeStamp = 0;
OSStatus result = MIDISend(pps->portRef,pps->endpoint,&packetList);
fprintf(stderr,"%d\n",result);

//MusicDeviceMIDIEvent(SynthUnits[pps->id], kMIDINoteOn, pps->tone[i] * (1 + curCmd.dir), level, 0);
MIDIPacketList packetList;

/*On*/
packetList.numPackets = 1;
packetList.packet[0].length = 3;
packetList.packet[0].data[0] = 0x90; // note ON
packetList.packet[0].data[1] = tone; // C
packetList.packet[0].data[2] = level; // velocity
packetList.packet[0].timeStamp = 0;
MIDISend(pps->portRef,pps->endpoint,&packetList);

usleep( pps->duration[i] );
packetList.numPackets = 1;
packetList.packet[0].length = 3;
packetList.packet[0].data[0] = kMIDINoteOff; // note ON
//packetList.packet[0].data[1] = 60 & 0x7F; // C
packetList.packet[0].data[1] = tone; // C
packetList.packet[0].data[2] = level; // velocity
packetList.packet[0].timeStamp = 0;
result = MIDISend(pps->portRef,pps->endpoint,&packetList);


// MusicDeviceMIDIEvent(SynthUnits[pps->id], kMIDINoteOff, pps->tone[i], level, 0);
/*Off*/
packetList.numPackets = 1;
packetList.packet[0].length = 3;
packetList.packet[0].data[0] = kMIDINoteOff; // note ON
packetList.packet[0].data[1] = tone; // C
packetList.packet[0].data[2] = level; // velocity
packetList.packet[0].timeStamp = 0;
MIDISend(pps->portRef,pps->endpoint,&packetList);

usleep( pps->period[i] );

}
Expand All @@ -236,59 +122,36 @@ int main(int argc, char *argv[])
}else{
NUM_SYNTHS = 8;
}
SynthNodes = (AUNode *)malloc(sizeof(AUNode) * NUM_SYNTHS);
SynthUnits = (AudioUnit*)malloc(sizeof(AudioUnit) * NUM_SYNTHS);

if( argc > 2 ){
g_level = atoi(argv[2]);
}else{
g_level = 64;
}

srand(time(NULL));
int i;
for(i = 0; i < 13; i++){
Keys[i].down = false;
Keys[i].tone = 0x41 + (i);
}

pthread_t sources[NUM_SYNTHS];
sequenceParams *sourceParams[NUM_SYNTHS];
int instruments[NUM_SYNTHS];


MIDIClientRef client;
OSStatus result = MIDIClientCreate(
CFStringCreateWithCString(NULL, "D-Ball's Client", kCFStringEncodingASCII),
NULL,
NULL,
&client);
MIDIClientRef client;
OSStatus result = MIDIClientCreate(
CFStringCreateWithCString(NULL, "D-Ball's Client", kCFStringEncodingASCII),
NULL,
NULL,
&client);

MIDIEndpointRef endpoint = MIDIGetDestination(0);

MIDIPortRef outputPort = NULL;
MIDIOutputPortCreate(client, CFSTR("My output port"), &outputPort);

//while (1) {
MIDIPacketList packetList;
packetList.numPackets = 1;
packetList.packet[0].length = 3;
packetList.packet[0].data[0] = 0x90; // note ON
packetList.packet[0].data[1] = 60 & 0x7F; // C
packetList.packet[0].data[2] = 127 & 0x7F; // velocity
packetList.packet[0].timeStamp = 0;
MIDIPacketList packetList;
packetList.numPackets = 1;
packetList.packet[0].length = 3;
packetList.packet[0].data[0] = 0x90; // note ON
packetList.packet[0].data[1] = 60 & 0x7F; // C
packetList.packet[0].data[2] = 127 & 0x7F; // velocity
packetList.packet[0].timeStamp = 0;

//printf("Sending...\n");
result = MIDISend(outputPort, endpoint, &packetList);
//printf("Got code %d\n", result);

//usleep(2000000);
//}
result = MIDISend(outputPort, endpoint, &packetList);


for(int i = 0; i < NUM_SYNTHS; i++){



MIDIEndpointRef e= MIDIGetDestination(i);

Expand All @@ -305,23 +168,19 @@ int main(int argc, char *argv[])

for( int j = 0; j < numsegs; j++ ){


s->tone[j] = 0x20 + (rand() % 20);
s->period[j] = (1000000 + (rand() % 3000000));
s->duration[j] = (1000000 + (rand() % 2000000));

}

instruments[i] = i*4;

//MusicDeviceMIDIEvent(SynthUnits[i], 0xC0, instruments[i], 0, 0);
pthread_create( &sources[i], NULL, sequence_play, (void *)s );

}

while(1){

fprintf(stderr,"MOTHERFUCKER\n");
int host;
int port;
int dir;
Expand All @@ -330,10 +189,8 @@ int main(int argc, char *argv[])

host = host % NUM_SYNTHS;
pthread_mutex_lock(&(sourceParams[host]->lock));
fprintf(stderr,"MOTHERFUCKER?\n");
if( sourceParams[host]->cmdBuf->full == false ){

fprintf(stderr,"MOTHERFUCKER was empty!\n");
/*full is false, so it is ok to put a command in*/
sourceParams[host]->cmdBuf->hostNum = host;
sourceParams[host]->cmdBuf->port = port;
Expand All @@ -343,7 +200,7 @@ int main(int argc, char *argv[])
}
pthread_mutex_unlock(&(sourceParams[host]->lock));

fprintf(stderr,"Done! MOTHERFUCKER!\n");

}

return(0);
Expand Down

0 comments on commit 3f25552

Please sign in to comment.