source: roaraudio/libroarpulse/sample.c @ 4960:60cdebcb83ef

Last change on this file since 4960:60cdebcb83ef was 4960:60cdebcb83ef, checked in by phi, 8 years ago

some updates for pulseaudio emulation, converted libroarpulse-simple fully to VS API

File size: 5.8 KB
Line 
1//sample.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
5 *  The code (may) include prototypes and comments (and maybe
6 *  other code fragements) from libpulse*. They are mostly copyrighted by:
7 *  Lennart Poettering <poettering@users.sourceforge.net> and
8 *  Pierre Ossman <drzeus@drzeus.cx>
9 *
10 *  This file is part of libroarpulse a part of RoarAudio,
11 *  a cross-platform sound system for both, home and professional use.
12 *  See README for details.
13 *
14 *  This file is free software; you can redistribute it and/or modify
15 *  it under the terms of the GNU General Public License version 3
16 *  as published by the Free Software Foundation.
17 *
18 *  RoarAudio is distributed in the hope that it will be useful,
19 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 *  GNU General Public License for more details.
22 *
23 *  You should have received a copy of the GNU General Public License
24 *  along with this software; see the file COPYING.  If not, write to
25 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
26 *  Boston, MA 02110-1301, USA.
27 *
28 *  NOTE for everyone want's to change something and send patches:
29 *  read README and HACKING! There a addition information on
30 *  the license of this document you need to read before you send
31 *  any patches.
32 *
33 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
34 *  or libpulse*:
35 *  The libs libroaresd, libroararts and libroarpulse link this libroar
36 *  and are therefore GPL. Because of this it may be illigal to use
37 *  them with any software that uses libesd, libartsc or libpulse*.
38 */
39
40#include <libroarpulse/libroarpulse.h>
41
42/** Return the amount of bytes playback of a second of audio with the specified sample type takes */
43size_t pa_bytes_per_second(const pa_sample_spec *spec) {
44 return pa_sample_size(spec) * spec->channels * spec->rate;
45}
46
47/** Return the size of a frame with the specific sample type */
48size_t pa_frame_size(const pa_sample_spec *spec) {
49 if ( spec == NULL )
50  return 0;
51
52 return pa_sample_size(spec) * spec->channels;
53}
54
55/** Return the size of a sample with the specific sample type */
56size_t pa_sample_size(const pa_sample_spec *spec) {
57 if ( spec == NULL )
58  return -1;
59
60 switch (spec->format) {
61  case PA_SAMPLE_ALAW:
62  case PA_SAMPLE_ULAW:
63  case PA_SAMPLE_U8:
64    return 1;
65   break;
66  case PA_SAMPLE_S16LE:
67  case PA_SAMPLE_S16BE:
68    return 2;
69   break;
70#ifdef PA_SAMPLE_S24LE
71  case PA_SAMPLE_S24LE:
72  case PA_SAMPLE_S24BE:
73    return 3;
74   break;
75#endif
76#ifdef PA_SAMPLE_S32LE
77  case PA_SAMPLE_S32LE:
78  case PA_SAMPLE_S32BE:
79  case PA_SAMPLE_S24_32LE:
80  case PA_SAMPLE_S24_32BE:
81    return 4;
82   break;
83#endif
84  default:
85    return 0;
86   break;
87 }
88
89 return 0;
90}
91
92/** Calculate the time the specified bytes take to play with the specified sample type */
93pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) {
94 if ( spec == NULL )
95  return 0;
96
97 return (pa_usec_t) (((double) length/pa_frame_size(spec)*1000000)/spec->rate);
98}
99
100/** Calculates the number of bytes that are required for the specified time. \since 0.9 */
101size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) {
102 if ( spec == NULL )
103  return 0;
104
105 return (size_t) (((double) t * spec->rate / 1000000))*pa_frame_size(spec);
106}
107
108/** Return non-zero when the sample type specification is valid */
109int pa_sample_spec_valid(const pa_sample_spec *spec) {
110 if ( spec == NULL )
111  return 0;
112
113 if ( spec->channels < 1 || spec->channels > ROAR_MAX_CHANNELS )
114  return 0;
115
116 if ( spec->format < 0 || spec->format > PA_SAMPLE_MAX )
117  return 0;
118
119 return 1;
120}
121
122/** Return non-zero when the two sample type specifications match */
123int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) {
124 if ( a->rate != b->rate )
125  return 0;
126
127 if ( a->channels != b->channels )
128  return 0;
129
130 if ( a->format != b->format )
131  return 0;
132
133 return 1;
134}
135
136/** Return a descriptive string for the specified sample format. \since 0.8 */
137static struct {
138 pa_sample_format_t format;
139 const char * name;
140} _roar_pa_format[] = {
141 {PA_SAMPLE_U8,        "u8"       },
142 {PA_SAMPLE_ALAW,      "aLaw"     },
143 {PA_SAMPLE_ULAW,      "uLaw"     },
144 {PA_SAMPLE_S16LE,     "s16le"    },
145 {PA_SAMPLE_S16BE,     "s16be"    },
146 {PA_SAMPLE_FLOAT32LE, "float32le"},
147 {PA_SAMPLE_FLOAT32BE, "float32be"},
148 {PA_SAMPLE_INVALID,   NULL       }
149};
150
151const char *pa_sample_format_to_string(pa_sample_format_t f) {
152 int i;
153
154 for (i = 0; _roar_pa_format[i].name != NULL; i++)
155  if ( _roar_pa_format[i].format == f )
156   return _roar_pa_format[i].name;
157
158 return NULL;
159}
160
161/** Parse a sample format text. Inverse of pa_sample_format_to_string() */
162pa_sample_format_t pa_parse_sample_format(const char *format) {
163 int i;
164
165 for (i = 0; _roar_pa_format[i].name != NULL; i++)
166  if ( !strcasecmp(_roar_pa_format[i].name, format) )
167   return _roar_pa_format[i].format;
168
169 return PA_SAMPLE_INVALID;
170}
171
172/** Maximum required string length for pa_sample_spec_snprint() */
173#define PA_SAMPLE_SPEC_SNPRINT_MAX 32
174
175/** Pretty print a sample type specification to a string */
176char* pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) {
177 if ( s == NULL || l == 0 || spec == NULL )
178  return NULL;
179
180 if ( pa_sample_spec_valid(spec) ) {
181  snprintf(s, l, "%s %uch %uHz", pa_sample_format_to_string(spec->format), spec->channels, spec->rate);
182 } else {
183  snprintf(s, l, "Invalid");
184 }
185
186 return s;
187}
188
189/** Pretty print a byte size value. (i.e. "2.5 MiB") */
190char* pa_bytes_snprint(char *s, size_t l, unsigned v) {
191 double val = v;
192 int    i;
193 const char pre[] = "KMGTP";
194
195 if ( v <= 1024 ) {
196  snprintf(s, l, "%u B", v);
197  return s;
198 }
199
200 for (i = 0; pre[i] != 0; i++) {
201  val /= 1024;
202  if ( val <= 1024 ) {
203   snprintf(s, l, "%0.1f %ciB", val, pre[i]);
204   return s;
205  }
206 }
207
208 snprintf(s, l, "%0.1f %ciB", val*1024., pre[i-1]);
209 return s;
210}
211
212//ll
Note: See TracBrowser for help on using the repository browser.