00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00031 #include "stdinc.h"
00032
00033 #include <sndfile.h>
00034
00035 #include "audio_file.h"
00036 #include "audio_format.h"
00037 #include "audio_output.h"
00038
00039 int
00040 sndfile_open(struct audio_file *fd, const char *ext)
00041 {
00042 SNDFILE *hnd;
00043 SF_INFO info;
00044 int fno;
00045
00046 if (fd->stream) {
00047
00048 return (-1);
00049 }
00050
00051
00052 fno = fileno(fd->fp);
00053 lseek(fno, 0, SEEK_SET);
00054
00055 if ((hnd = sf_open_fd(fno, SFM_READ, &info, 0)) == NULL)
00056 return (-1);
00057 fd->drv_data = (void *)hnd;
00058
00059 fd->srate = info.samplerate;
00060 fd->channels = info.channels;
00061 fd->time_len = info.frames / fd->srate;
00062
00063
00064 fd->artist = g_strdup(sf_get_string(hnd, SF_STR_ARTIST));
00065 fd->title = g_strdup(sf_get_string(hnd, SF_STR_TITLE));
00066 fd->album = g_strdup(sf_get_string(hnd, SF_STR_ALBUM));
00067
00068 return (0);
00069 }
00070
00071 void
00072 sndfile_close(struct audio_file *fd)
00073 {
00074 SNDFILE *hnd = fd->drv_data;
00075 sf_close(hnd);
00076 }
00077
00078 size_t
00079 sndfile_read(struct audio_file *fd, int16_t *buf, size_t len)
00080 {
00081 SNDFILE *hnd = fd->drv_data;
00082 sf_count_t ret, frame;
00083
00084 ret = sf_read_short(hnd, buf, len);
00085
00086
00087 frame = sf_seek(hnd, 0, SEEK_CUR);
00088 fd->time_cur = frame / fd->srate;
00089
00090 return (ret);
00091 }
00092
00093 void
00094 sndfile_seek(struct audio_file *fd, int len, int rel)
00095 {
00096 SNDFILE *hnd = fd->drv_data;
00097 sf_count_t frame;
00098
00099
00100 if (rel)
00101 len += fd->time_cur;
00102 len = CLAMP(len, 0, (int)fd->time_len);
00103
00104 frame = sf_seek(hnd, len * fd->srate, SEEK_SET);
00105 fd->time_cur = frame / fd->srate;
00106 }