chiark / gitweb /
Imported Upstream version 1.0.0
[e16] / src / sound_load.c
1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  * Copyright (C) 2004-2008 Kim Woelders
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies of the Software, its documentation and marketing & publicity
14  * materials, and acknowledgment shall be given in the documentation, materials
15  * and software packages that this Software was used.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include "E.h"
25 #if HAVE_SOUND
26 #if USE_SOUND_LOADER_AUDIOFILE
27 #include "sound.h"
28 #include <audiofile.h>
29
30 int
31 SoundSampleGetData(const char *file, SoundSampleData * ssd)
32 {
33    AFfilehandle        in_file;
34    int                 in_format, in_width, bytes_per_frame, frame_count;
35    int                 frames_read;
36
37    in_file = afOpenFile(file, "r", NULL);
38    if (!in_file)
39       return -1;
40
41    frame_count = afGetFrameCount(in_file, AF_DEFAULT_TRACK);
42    ssd->channels = afGetChannels(in_file, AF_DEFAULT_TRACK);
43    ssd->rate = (unsigned int)(afGetRate(in_file, AF_DEFAULT_TRACK) + .5);
44    afGetSampleFormat(in_file, AF_DEFAULT_TRACK, &in_format, &in_width);
45    ssd->bit_per_sample = in_width;
46 #ifdef WORDS_BIGENDIAN
47    afSetVirtualByteOrder(in_file, AF_DEFAULT_TRACK, AF_BYTEORDER_BIGENDIAN);
48 #else
49    afSetVirtualByteOrder(in_file, AF_DEFAULT_TRACK, AF_BYTEORDER_LITTLEENDIAN);
50 #endif
51    if (EDebug(EDBUG_TYPE_SOUND))
52       Eprintf("SoundSampleGetData chan=%d width=%d rate=%d\n", ssd->channels,
53               ssd->bit_per_sample, ssd->rate);
54
55    bytes_per_frame = (ssd->bit_per_sample * ssd->channels) / 8;
56    ssd->size = frame_count * bytes_per_frame;
57    ssd->data = EMALLOC(unsigned char, ssd->size);
58
59    frames_read =
60       afReadFrames(in_file, AF_DEFAULT_TRACK, ssd->data, frame_count);
61
62    afCloseFile(in_file);
63
64    if (frames_read <= 0)
65      {
66         ssd->size = 0;
67         _EFREE(ssd->data);
68         return -1;
69      }
70
71    return 0;
72 }
73
74 #endif /* USE_SOUND_LOADER_AUDIOFILE */
75
76 #endif /* HAVE_SOUND */