#!/usr/bin/sh
# 
# Write soud files to the Akai S3000 disk in the Akai's native format.
# Copyright (c) 1997, Hiroyuki Ohsaki.
# All rights reserved.
# 
# $Id: akaiconv,v 1.2 1998/06/30 01:35:15 oosaki Exp ohsaki $
# 

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program 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 General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

prog=`basename $0`

usage() {
  echo "usage: $prog [-v] [-f file] [-p part] [-d dir] [-e file] file..."
  exit 2
}

umask 077

# parse options
while getopts 'vd:f:p:e:' i
do
  case $i in 
    v) verb='-v' ;;
    f) file="-f $OPTARG" ;;
    p) part="-p $OPTARG" ;;
    d) vol="$OPTARG" ;;
    e) eps=$OPTARG ;;
    *) usage ;;
  esac
done
shift `expr $OPTIND - 1`

# sound files must be given
if [ $# = 0 ]; then
  usage;
fi

# if the direcoty is not specified, use AKAICONV_XXX instead
if [ "$vol" = "" ]; then
  i=`akailist $file $part | grep -i $prog | wc -l`
  i=`expr $i + 1`
  vol=`printf "$prog %03d" $i`
fi

# create a temporary directory
tmpdir=/tmp/$prog$$
trap 'rm -rf $tmpdir; exit 2' 1 2 15
mkdir $tmpdir

for i in $*
do
  # clean temporary files
  rm -f $tmpdir/*

  # if -e option is specified, read the file from EPS HDD/CD-ROM
  if [ "$eps" != "" ]; then
    epsread $verb -f $eps -d $tmpdir $i
    i=`ls $tmpdir/* | head -1`
  fi

  # convert the file to Akai programs/samples
  any2akai $verb -d $tmpdir $i

  # create a new directory if the file is in EPS format
  case $i in
    *.ins|*.INS)
      vol=`echo $i | sed -e 's/^.*\///' -e 's/\.[^.]*//' \
	-e 's/[^0-9 A-Za-z#.+-]/ /g'`
      create=
      ;;
  esac

  # create the destination directory if not exists
  if [ "$create" = "" ]; then
    akaimkdir $file $part "$vol"
    create=1
  fi

  # write all progs/samples to the disk
  akaiwrite $verb $file $part -d "$vol" $tmpdir/*.a3?
done

# clear away the temporary directory
rm -rf $tmpdir
exit 0

cat <<EOF >/dev/null

=head1 NAME

akaiconv - convert and write various sound files to disk in Akai format

=head1 SYNOPSIS

  akaiconv [-v] [-f file] [-p part] [-d dir] [-e file] path...

=head1 DESCRIPTION

This manual page documents B<akaiconv>.  This program is a shell
script to automatically convert various sound files to Akai format,
and write it to the Akai disk by using other programs.

By default, B<akaiconv> converts I<file> to Akai S3000 sample, and
saves it in the disk of Akai format .  B<akaiconv> recognizes C<-f>
and C<-p> options, which are identical to other B<akaitools> such as
B<akailistf>, B<akairead> and B<akaiwrite>.

Since B<akaiconv> internally calls B<any2akai> and B<any2akai>
utilizes B<sox>, B<akaiconv> can accept all formats supported by
B<sox> in addition to I<Ensoniq EPS instrument> and I<Microsoft RIFF
WAVE> formats.  See L<sox(1)> for supported sound formats of B<sox>.

=head1 OPTIONS

=over 4

=item C<-v>

Verbose mode.  Print much information for debugging.

=item C<-f> file

Specify the regular file of Akai disk image or the block device file
of Akai HDD/CD-ROM as I<file>.  Usually, this should be the block
device of HDD or CD-ROM such as F</dev/sda>.  If this option is
omitted, the environment variable I<AKAI_DISK> is used as the default
device.  If I<AKAI_DISK> is not defined, F</dev/mo> is assumed.

=item C<-p> parition

Specify the partition number for which B<akailist> works.  The first
partition is 1.  By default, the first partition is used.

=item C<-d> directory

Specify the directory in which converted samples will be saved.  If
the directory does not exist, B<akaiconv> will create it.  By default, 
B<akaiconv> saves samples in the directory F<AKAICONV XXX>, where
F<XXX> is chosen to be larger-by-one than existing directories.

=item C<-e> file

When this option is given, sound files are extracted from HDD/CD-ROM
in Ensoniq format attached at I<file>.  In this case, I<path> is
regarded as the pathname of Ensoniq HDD/CD-ROM (see L<epslist>).

=back

=head1 EXAMPLES

This section shows several example usage of B<akaiconv>.

=over 4

=item - Convert and save F<foo.wav> in the HDD in Akai format

  akaiconv *.wav 

=item - Convert and save all AIFF sound files in the directory,
F</AIFF FILES>, of the second partition.

  akaiconv -p2 -d /aiff_files *.aiff

=item - Convert and write all instrument files in the Ensoniq CD-ROM.

  epslist -f /dev/cdrom -ulR | grep Instrument | \
    awk '{ print $NF }' | xargs akaiconv -e /dev/cdrom 

=back

=head1 SEE ALSO

akaitools(1), akaiformat(1), akailist(1), akaimkdir(1), akairead(1),
akaiwrite(1), epslist(1), epsread(1), eps2akai(1), wav2akai(1),
any2akai(1), akaiconv(1) 

=head1 AUTHOR

Hiroyuki Ohsaki <ohsaki@lsnl.jp>

=cut

EOF
