prefect
Home Engineering Publications
DACS: Main Program Code, midictrl.c
 
  next up previous contents
Next: MIDI Handler Header, midi.h Up: MIDI Controller Previous: MIDI Controller   Contents

Main Program Code, midictrl.c

This code is the main MIDI control program. It simply exchanges MIDI-like information over serial ports between the DACS control board, mixer unit, and a separate host PC running a MIDI sequencing package and a special serial port MIDI driver.
/*****************************************************************************
 * DACS : Distributed Audio Control System
 *============================================================================
 *         File: midictrl.c
 *       Author: Stephen S. Richardson
 * Date Created: 04.18.97
 *  Environment: GNU C Compiler (GCC) v2.7.1, Linux i486 v2.0.28
 *        Build: make
 *============================================================================
 * The code, executables, documentation, firmware images, and all related
 * material of DACS are  
 * Copyright (C) 1997 Stephen S. Richardson - ALL RIGHTS RESERVED
 *****************************************************************************/

/*
 * most of this code is a pretty ugly hack.. it was written hastily to get
 * a demo working for presentation day.
 *
 * implements "dumb" mode of control board, mixer unit, cdrom
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netdb.h>
#include "midi.h"
#include "mixer.h"
#include "client.h"
#include "cdaudio_comm.h"

void oops (char *mesg)
{
  perror (mesg);
  exit (1);
}

void main (void)
{
  struct midi_stream board, mixer, midi;
  struct mix_control mctrl;
  unsigned char ch, obuf[10];
  int r;
  int sock_cd;
  char buf_cd[CD_TCP_BUFSIZE];
  struct cdtype *cd;

  printf ("DACS : Distributed Audio Control System\nCopyright (C) 1997 Stephen S. Richardson / SR Engineering\n\n");

  /* connect with CD audio server */

  printf ("Contacting CD audio server...");
  sock_cd = tcpEstablishConn ("localhost", CD_TCP_PORT);
  if (sock_cd<0) oops ("tcpEstablishConn (cd audio)");

  printf ("OK.\n");

  cd=(struct cdtype *) buf_cd;


  /* open serial & MIDI devices */
  midi.fd=midi_openser("/dev/cua1", B38400);  /* Windows generic serial MIDI */
  mixer.fd=midi_openser("/dev/cua2", B9600);  /* DACS 411 mixer serial MIDI */
  board.fd=midi_openser("/dev/cua0", B9600);  /* DACS 112 ctrlr serial MIDI */

  if (!midi.fd) oops ("can't open midi device...");
  if (!mixer.fd) oops ("can't open mixer device...");
  if (!board.fd) oops ("can't open board device...");



  mixer_reset(&mixer);
  for (r=0;r<128;r++) mctrl.chipreg[r]=0xff;

  while (1) {

    if (midi_datawaiting(&midi)) {
      /* there's data waiting from the MIDI host */

      midi_readstream (&midi);   /* get it.. */

      if (midi.validdata) {
	/* we've received a whole valid command */

	switch (midi.cmd) {
	case MIDICMD_CONTROL:

	  /* it was a MIDI controller message */

	  if (midi.chan==0x0F) {

	    /* hardcoded hack: if it's channel 15, it's going to the mixer */

	    printf ("MIDI controller %x to value %x on channel %x\n", midi.data[0], midi.data[1], midi.chan);

	    /* change the controller table */

	    mctrl.midictrl[midi.data[0]-1]=midi.data[1];

	    /* call the controller translate routine */

	    controller_translate (&mctrl, mixer.fd);
	  }
	  break;

	case MIDICMD_PROGRAM:

	  /* MIDI program change */

	  if (midi.chan==0x0E) {

	    /* another egregious hack, if it's on channel 14, we control the
	       CD-ROM, and choose which disc to load */

	    printf ("MIDI program change to %d on channel %x\n", midi.data[0], midi.chan);
	    
	    /* the cd data structure.. mangle appropriate elements */

	    cd->disc=midi.data[0];
	    cd->function=CD_OPEN;
	    
	    /* talk to CDROM server */

	    tcpWriteBuffer (sock_cd, buf_cd, CD_TCP_BUFSIZE);
	  }
	  break;

	case MIDICMD_NOTEON:

	  /* MIDI note on command */

	  if (midi.chan==0x0E) {

	    /* yes, another hack for the demo.. note-on starts a track on
	       the current cd */

	    printf ("MIDI note %d on with velocity %d on channel %d\n", midi.data[0], midi.data[1], midi.chan);

	    cd->function=CD_PLAY;
	    cd->track=midi.data[0]+1;
	    cd->s_min=0;
	    cd->s_sec=1;
	    cd->d_min=73;
	    cd->d_sec=0;
	    tcpWriteBuffer (sock_cd, buf_cd, CD_TCP_BUFSIZE);

	  }
	  break;
	}
      }
    } else {

      switch (midi.runstatus) {
      case MSTOP:
	printf ("MIDI stop\n");

	/* when the MIDI stop code is received, stop all of our running
	   functions */

	mixer_reset (&mixer);
	midi.runstatus = 0;	
	cd->function=CD_STOP;
	tcpWriteBuffer (sock_cd, buf_cd, CD_TCP_BUFSIZE);
	cd->function=CD_CLOSE;
	tcpWriteBuffer (sock_cd, buf_cd, CD_TCP_BUFSIZE);
	break;
      case MSTART:
	printf ("MIDI start\n");

	/* when the MIDI start code is received, reset some stuff.. */

	mixer_reset (&mixer);
	midi.runstatus = 1;
	break;
      case MCONT:
	break;
      }

      if (midi_datawaiting(&board)) {

	/* there's data waiting on the board's stripped-MIDI channel */

	midi_readstream (&board);
	
	if (board.validdata) {
	  switch (board.cmd) {
	  case MIDICMD_CONTROL:
	    
	    if (board.chan==0x00) {
	      printf ("BOARD controller %x to value %x on channel %x\n", board.data[0], board.data[1], board.chan);

	      /* send this to the MIDI host on channel 15 */

	      obuf[0]=0xBF;
	      obuf[1]=board.data[0]+1;
	      obuf[2]=board.data[1];

	      write(midi.fd, obuf, 3);
	      
	    }
	    break;

	  }  
	}
      } else {
      }
      

    }

  }	       

}


Steve Richardson 2000-07-06
Table of Contents

[PDF] [Whole document in PDF 1.9MB]

[more photos and information]

 
Page last modified:
Copyright © 1993-2000 prefect - All Rights Reserved.