packaging: bumped version, updated changelog.
[profile/ivi/speech-recognition.git] / src / daemon / audiobuf.c
1 #include <murphy/common/mm.h>
2 #include <murphy/common/refcnt.h>
3
4 #include "srs/daemon/audiobuf.h"
5
6 /*
7  * audio buffer handling
8  */
9
10 srs_audiobuf_t *srs_create_audiobuf(srs_audioformat_t format, uint32_t rate,
11                                     uint8_t channels, size_t samples,
12                                     void *data)
13 {
14     srs_audiobuf_t *buf;
15     size_t          width, size;
16
17     switch (format) {
18     case SRS_AUDIO_U8:
19     case SRS_AUDIO_ALAW:
20     case SRS_AUDIO_ULAW:
21         width = 1;
22         break;
23     case SRS_AUDIO_S16LE:
24     case SRS_AUDIO_S16BE:
25         width = 2;
26         break;
27     case SRS_AUDIO_FLOAT32LE:
28     case SRS_AUDIO_FLOAT32BE:
29         width = sizeof(float);
30         break;
31     case SRS_AUDIO_S32LE:
32     case SRS_AUDIO_S32BE:
33     case SRS_AUDIO_S24_32LE:
34     case SRS_AUDIO_S24_32BE:
35         width = 4;
36         break;
37     case SRS_AUDIO_S24LE:
38     case SRS_AUDIO_S24BE:
39         width = 3;
40
41     default:
42         return NULL;
43     }
44
45     size = channels * samples * width;
46
47     if ((buf = mrp_allocz(sizeof(*buf))) != NULL) {
48         if ((buf->data = mrp_datadup(data, size)) != NULL) {
49             mrp_refcnt_init(&buf->refcnt);
50             buf->format   = format;
51             buf->rate     = rate;
52             buf->channels = channels;
53             buf->samples  = samples;
54
55             return buf;
56         }
57         else
58             mrp_free(buf);
59     }
60
61     return NULL;
62 }
63
64
65 srs_audiobuf_t *srs_ref_audiobuf(srs_audiobuf_t *buf)
66 {
67     return mrp_ref_obj(buf, refcnt);
68 }
69
70
71 void srs_unref_audiobuf(srs_audiobuf_t *buf)
72 {
73     if (mrp_unref_obj(buf, refcnt)) {
74         mrp_free(buf->data);
75         mrp_free(buf);
76     }
77 }