00001 /*************************************************************************** 00002 * Copyright (C) 2006 by André Lindhjem <belgarat@sdf.lonestar.org>, * 00003 * Kjetil Holien <kjetil.holien@gmail.com>, * 00004 * Terje Risa <terje.risa@gmail.com> & * 00005 * Øyvind Nerbråten <oyvind@nerbraten.com> * 00006 * * 00007 * This program is free software; you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation; either version 2 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program; if not, write to the * 00019 * Free Software Foundation, Inc., * 00020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00021 *************************************************************************** 00022 * 00023 * \file audio.h 00024 * \author André Lindhjem, Kjetil Holien, Terje Risa & Øyvind Nerbråten 00025 * \date 25.02.2006 00026 * 00027 * Audio module. This handles the task of reading audio files and playing 00028 * the audio output. It uses a MP3-decoder and libao, and it can easilly be 00029 * extended with more decoders. 00030 */ 00031 00032 00033 #ifndef AUDIO_H_ 00034 #define AUDIO_H_ 00035 00036 #include <mad.h> /* mad_timer_t */ 00037 #include <ao/ao.h> /* ao_sample_format */ 00038 #include <sys/stat.h> /* file stat */ 00039 #include <pthread.h> 00040 00041 #include "daisylibao.h" 00042 #include "libdaisy.h" 00043 00044 00045 /* ************************************************ * 00046 * Global type definitions and datastructures * 00047 * ************************************************ */ 00048 00054 typedef void * audio_data_t; 00055 00059 typedef enum AUDIO_FILE_TYPE 00060 { 00061 AUDIO_FILE_WAV, /* PCM WAVE file */ 00062 AUDIO_FILE_MP3, /* MP3 file. uses mp3.h */ 00063 AUDIO_FILE_AAC, /* AAC file. uses aac.h */ 00064 AUDIO_FILE_UNKNOWN /* Unknown filetype */ 00065 } audio_file_t; 00066 00071 typedef enum { 00072 AUDIO_STATE_PLAY, 00073 AUDIO_STATE_PAUSE, 00074 AUDIO_STATE_STOP, 00075 AUDIO_STATE_NOOP 00076 } audio_state_t; 00077 00082 typedef struct 00083 { 00084 /* buffer */ 00085 struct buffer 00086 { 00087 unsigned char const *buffer_start; 00088 unsigned long buffer_length; 00089 } buffer; 00090 00091 char *file_name; /* the file_name of the file we have loaded */ 00092 char *prev_file_name; /* the previous file_name. we check against this so that we won't have to reload already opened files */ 00093 00094 int fd; /* input file descriptor */ 00095 void *fdm; /* pointer to file mmap */ 00096 struct stat stat; /* file stats */ 00097 00098 audio_file_t file_type; /* the file_type (mp3, aac, wav) */ 00099 audio_file_t prev_file_type; /* the previous file_type */ 00100 00101 ao_sample_format format; /* the format of our output samples. we use this to initialize ao */ 00102 00103 /* mad_timer_t variables to keep track of progress, start and stop-times. */ 00104 /* TODO: We'll have to use this for all decoders. Define our own struct in audio.h and use this instead? This should be fixed before additional decoders are implemented. */ 00105 mad_timer_t progress; 00106 mad_timer_t start; 00107 mad_timer_t stop; 00108 00109 audio_state_t state; 00110 bool is_playing; 00111 00112 bool first_run; /* we use this to find out if a decoder iteration is our first or not. */ 00113 00114 /* thread data */ 00115 pthread_mutex_t mutex; /* we need to prevent race conditions on shared variables */ 00116 pthread_cond_t unpause_cond; 00117 00118 /* callback functions */ 00119 void (*cb_playing_done) (daisyplayer_t d); /* audio done callback function */ 00120 bool do_callback_done; /* set to true to run cb_playing_done once */ 00121 void (*cb_error) (void *data, enum daisy_status, const char *); /* error callback */ 00122 void (*cb_progress) (void *data, long int progress_ms); /* progress callback */ 00123 00124 daisyplayer_t *daisy; /* this will need to be passed along when callback function cb_playing_done is called */ 00125 00126 struct audio_dither *mp3_dither; /* Dither struct. This is used by the mp3.c dither routine */ 00127 00128 ao_device *device; /* device is used by ao. */ 00129 00130 unsigned int magic; /* magic number */ 00131 } struct_audio_data_t; 00132 00133 00134 00135 /* ************************************************ * 00136 * Global function declarations * 00137 * ************************************************ */ 00138 00147 unsigned 00148 long 00149 int audio_play (audio_data_t _data, const char *fname, const char *start, const char *stop); 00150 00157 int audio_pause (audio_data_t _data); 00158 00165 int audio_stop (audio_data_t _data); 00166 00173 int callback (audio_data_t _data, long int); 00174 00180 void audio_terminate (audio_data_t _data); 00181 00191 audio_data_t audio_initiate (daisyplayer_t daisy, 00192 void (*done) (daisyplayer_t d), 00193 void (*error) (void *data, enum daisy_status, const char *), 00194 void (*progress) (void *data, long int progress_ms)); 00195 00203 void *audio_thread (void *_data); 00204 00210 audio_state_t audio_get_state (audio_data_t _data); 00211 00212 #endif /* AUDIO_H_ */