/* tools.c */

/* standard routines for use in tracker. Used to be in main.c
 */

/* $Author: espie $
 * $Id: tools.c,v 1.1 1992/07/23 13:52:28 espie Exp espie $
 * $Log: tools.c,v $
 * Revision 1.1  1992/07/23  13:52:28  espie
 * Initial revision
 *
 */
     

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
     
#include "defs.h"
#include "extern.h"
     
LOCAL char *id = "$Id: tools.c,v 1.1 1992/07/23 13:52:28 espie Exp espie $";


LOCAL BOOL opened = FALSE;
LOCAL int ask_freq, real_freq, oversample;
LOCAL BOOL stereo;

/* setup_audio(frequency, stereo, oversample) */
void setup_audio(f, s, o)
int f;
BOOL s;
int o;
    {

    if (!opened)
        {
        ask_freq = f;
        stereo = s;
        oversample = o;
        real_freq = open_audio(ask_freq, stereo);
        init_player(oversample, real_freq);
        opened = TRUE;
        }
    else
        {
        int new_freq;

        if (s != stereo || f != ask_freq)
            {
            ask_freq = f;
            stereo = s;
            close_audio();
            new_freq = open_audio(ask_freq, stereo);
            }
        else
            new_freq = real_freq;

        if (new_freq != real_freq || oversample != o)
            {
            real_freq = new_freq;
            oversample = o;
            init_player(oversample, real_freq);
            }
        }
    }

/* v = read_env(name, default): reads the scalar value v
 * in the environment, supplies a defaults.
 */
int read_env(name, def)
char *name;
int def;
    {
    char *var;
    int value;

    var = getenv(name);
    if (!var)
        return def;
    if (sscanf(var, "%d", &value) == 1)
        return value;
    else
        return def;
    }

struct song *do_read_song(name, type, transpose)
char *name;
int type;
int transpose;
    {
    struct song *song;
    FILE *file;

    printf("(%s)...", name);
    fflush(stdout);

    file = open_file(name, "r", getenv("MODPATH"));
    if (!file)
        {
        fprintf(stderr, "Unable to open tune file %s\n", name);
        return NULL;
        }
    song = read_song(file, type, transpose); 
    close_file(file);
    if (song)
        printf("Song loaded Ok!\n");
    return song;
    }

