prefect
Home Engineering Publications
DACS: CDROM Service Provider CD Function Code, cdaudio_func.c
 
  next up previous contents
Next: Serial Communications Header, serial.h Up: MIDI Controller Previous: CDROM Service Provider Main   Contents

CDROM Service Provider CD Function Code, cdaudio_func.c

This code handles talking to the CD-ROM device driver. This code is relatively specific to the Linux environment.
/*****************************************************************************
 * DACS : Distributed Audio Control System
 *============================================================================
 *         File: cdaudio_func.c
 *       Author: Stephen S. Richardson
 * Date Created: 04.22.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
 *****************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <linux/cdrom.h>
#include "cdaudio_func.h"

struct cdhw_t *read_hw (int cdfile, int *err)
{
  int i;
  struct cdhw_t *hw = malloc(sizeof(struct cdhw_t));

  /* read header */
  if ( ioctl(cdfile, CDROMREADTOCHDR, &(hw->tochdr)) == -1 ) {
    *err=ERROR_CDROMHW;
    return(NULL);
  }
  
  /* read individual tracks */
  for (i=hw->tochdr.cdth_trk0; i<=hw->tochdr.cdth_trk1; i++) {
    hw->tocentries[i-1].cdte_track = i;
    hw->tocentries[i-1].cdte_format = CDROM_MSF;
    if ( ioctl(cdfile, CDROMREADTOCENTRY, &(hw->tocentries[i-1])) == -1 ) {
      *err=ERROR_CDROMHW;
      return (NULL);
    }
  }
  
  /* read the lead-out track */
  hw->tocentries[hw->tochdr.cdth_trk1].cdte_track = CDROM_LEADOUT;
  hw->tocentries[hw->tochdr.cdth_trk1].cdte_format = CDROM_MSF;
  if ( ioctl(cdfile, CDROMREADTOCENTRY, &(hw->tocentries[hw->tochdr.cdth_trk1])) == -1 ) {
    *err=ERROR_CDROMHW;
    return (NULL);
  }
  
  /* read subchannel info */
  hw->subchnl.cdsc_format = CDROM_MSF;
  if ( ioctl(cdfile, CDROMSUBCHNL, &(hw->subchnl)) == -1 ) {
    *err=ERROR_CDROMHW;
    return (NULL);
  }
  
  *err=SUCCESS;
  return (hw);
 
}


int cdrPlay (int track, int seekmin, int seeksec, int durmin, int dursec, int cdfile, struct discinfo *di)
{
  int t;
  struct cdrom_tochdr tochdr;
  struct cdrom_msf msf;
  struct cdrom_subchnl subchnl;

  subchnl.cdsc_format = CDROM_MSF;

  if (ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1) {
    return (ERROR_CDROMHW);
  }

  if (subchnl.cdsc_audiostatus == CDROM_AUDIO_PAUSED) {
    if ( ioctl(cdfile, CDROMRESUME) == -1 ) {
      return (ERROR_CDROMHW);
    }
  } else {
    if ( ioctl(cdfile, CDROMREADTOCHDR, &tochdr) == -1 ) {
      return (ERROR_CDROMHW);
    }

    t=(seekmin*60)+seeksec+di->tsec[track];

    msf.cdmsf_min0 = t/60;
    msf.cdmsf_sec0 = t-(t/60)*60;
    msf.cdmsf_frame0 = 1;

    if ( (durmin==0) && (dursec==0) ) {
      t=di->tsec[track+1];
      
      msf.cdmsf_min1 = t/60;
      msf.cdmsf_sec1 = t-(t/60)*60;
      msf.cdmsf_frame1 = 1;
    } else {
      t=(durmin*60)+(dursec)+di->tsec[track];
      
      msf.cdmsf_min1 = t/60;
      msf.cdmsf_sec1 = t-(t/60)*60;
      msf.cdmsf_frame1 = 1;
    }

    if ( ioctl(cdfile, CDROMPLAYMSF, &msf) == -1 ) {
      return (ERROR_CDROMHW);                             
    }
  }
  return (SUCCESS);

}

int cdrCue (int track, int seekmin, int seeksec, int durmin, int dursec, int cdfile, struct discinfo *di)
{
  int t;
  struct cdrom_tochdr tochdr;
  struct cdrom_msf msf;
  struct cdrom_subchnl subchnl;

  subchnl.cdsc_format = CDROM_MSF;

  if (ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1) {
    return (ERROR_CDROMHW);
  }

  if (subchnl.cdsc_audiostatus == CDROM_AUDIO_PAUSED) {
    if ( ioctl(cdfile, CDROMRESUME) == -1 ) {
      return (ERROR_CDROMHW);
    }
  } else {
    if ( ioctl(cdfile, CDROMREADTOCHDR, &tochdr) == -1 ) {
      return (ERROR_CDROMHW);
    }


    t=(seekmin*60)+seeksec+di->tsec[track];

    msf.cdmsf_min0 = t/60;
    msf.cdmsf_sec0 = t-(t/60)*60;
    msf.cdmsf_frame0 = 1;

    if ( (durmin==0) && (dursec==0) ) {
      t=di->tsec[track+1];
      
      msf.cdmsf_min1 = t/60;
      msf.cdmsf_sec1 = t-(t/60)*60;
      msf.cdmsf_frame1 = 1;
    } else {
      t+=(durmin*60)+dursec;
      
      msf.cdmsf_min1 = t/60;
      msf.cdmsf_sec1 = t-(t/60)*60;
      msf.cdmsf_frame1 = 1;
    }

    if ( ioctl(cdfile, CDROMPLAYMSF, &msf) == -1 ) {
      return (ERROR_CDROMHW);                             
    }

    if ( ioctl(cdfile, CDROMPAUSE) == -1 ) {
      return (ERROR_CDROMHW);
    }
  }
  return (SUCCESS);
}

int cdrPause (int cdfile)
{
  if ( ioctl(cdfile, CDROMPAUSE) == -1 ) {
    return (ERROR_CDROMHW);
  }
  return (SUCCESS);
}

int cdrResume (int cdfile)
{
  if ( ioctl(cdfile, CDROMRESUME) == -1 ) {
    return (ERROR_CDROMHW);
  }
  return (SUCCESS);
}

int cdrStop (int cdfile)
{
  if ( ioctl(cdfile, CDROMSTOP) == -1 ) {
    return (ERROR_CDROMHW);
  }
  return (SUCCESS);
}

int cdrEject (int cdfile)
{
  if ( ioctl(cdfile, CDROMEJECT) == -1 ) {
    return (ERROR_CDROMHW);
  }
  return (SUCCESS);
}


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.