• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __AUDIO_UTIL_SNDFILE_H
18 #define __AUDIO_UTIL_SNDFILE_H
19 
20 // This is a C library for reading of PCM .wav and writing of PCM .wav and .raw files. It is
21 // influenced by other libraries such as libsndfile and audiofile, except is
22 // much smaller and has an Apache 2.0 license.
23 // The API should be familiar to clients of similar libraries, but there is
24 // no guarantee that it will stay exactly source-code compatible with other libraries.
25 
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <sys/cdefs.h>
29 #include <system/audio.h>
30 
31 /** \cond */
32 __BEGIN_DECLS
33 /** \endcond */
34 
35 // visible to clients
36 typedef int sf_count_t;
37 
38 typedef struct {
39     sf_count_t frames;
40     int samplerate;
41     int channels;
42     int format;
43 } SF_INFO;
44 
45 // opaque to clients
46 typedef struct SNDFILE_ SNDFILE;
47 
48 // Access modes
49 #define SFM_READ    1
50 #define SFM_WRITE   2
51 
52 // Format
53 #define SF_FORMAT_TYPEMASK  1
54 #define SF_FORMAT_WAV       1 // without this bit set, raw data is written
55 #define SF_FORMAT_SUBMASK   14
56 #define SF_FORMAT_PCM_16    2
57 #define SF_FORMAT_PCM_U8    4
58 #define SF_FORMAT_FLOAT     6
59 #define SF_FORMAT_PCM_32    8
60 #define SF_FORMAT_PCM_24    10
61 
62 /** Open stream */
63 SNDFILE *sf_open(const char *path, int mode, SF_INFO *info);
64 
65 /** Close stream */
66 void sf_close(SNDFILE *handle);
67 
68 /**
69  * Read interleaved frames
70  * \return actual number of frames read
71  */
72 sf_count_t sf_readf_short(SNDFILE *handle, int16_t *ptr, sf_count_t desired);
73 sf_count_t sf_readf_float(SNDFILE *handle, float *ptr, sf_count_t desired);
74 sf_count_t sf_readf_int(SNDFILE *handle, int *ptr, sf_count_t desired);
75 
76 /**
77  * Write interleaved frames
78  * \return actual number of frames written
79  */
80 sf_count_t sf_writef_short(SNDFILE *handle, const int16_t *ptr, sf_count_t desired);
81 sf_count_t sf_writef_float(SNDFILE *handle, const float *ptr, sf_count_t desired);
82 sf_count_t sf_writef_int(SNDFILE *handle, const int *ptr, sf_count_t desired);
83 
84 /**
85  * Map SF_FORMAT to PCM format
86  */
SF_format_to_audio_format(int format)87 static inline audio_format_t SF_format_to_audio_format (int format) {
88     // Ignore FORMAT_WAV bit
89     switch (format & (~SF_FORMAT_WAV)) {
90     case SF_FORMAT_PCM_16:
91         return AUDIO_FORMAT_PCM_16_BIT;
92     case SF_FORMAT_PCM_U8:
93         return AUDIO_FORMAT_PCM_8_BIT;
94     case SF_FORMAT_FLOAT:
95         return AUDIO_FORMAT_PCM_FLOAT;
96     case SF_FORMAT_PCM_32:
97         return AUDIO_FORMAT_PCM_32_BIT;
98     case SF_FORMAT_PCM_24:
99         return AUDIO_FORMAT_PCM_24_BIT_PACKED;
100     default:
101         return AUDIO_FORMAT_INVALID;
102     }
103 }
104 
105 /** \cond */
106 __END_DECLS
107 /** \endcond */
108 
109 #endif /* __AUDIO_UTIL_SNDFILE_H */
110