summaryrefslogtreecommitdiffstats
path: root/tinyMEDIA/src/tmedia_resampler.c
blob: d4113de238de27f5fadf643ec7bb92666750afcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (C) 2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tmedia_resampler.c
 * @brief Audio Resample plugin
 *
 * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
 */
#include "tinymedia/tmedia_resampler.h"

#include "tsk_debug.h"

static const tmedia_resampler_plugin_def_t* __tmedia_resampler_plugin = tsk_null;

int tmedia_resampler_init(tmedia_resampler_t* self)
{
    if(!self) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    return 0;
}

int tmedia_resampler_open(tmedia_resampler_t* self, uint32_t in_freq, uint32_t out_freq, uint32_t frame_duration, uint32_t in_channels, uint32_t out_channels, uint32_t quality, uint32_t bits_per_sample)
{
    int ret;

    if (!self || !self->plugin || !self->plugin->open) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    if (self->opened) {
        TSK_DEBUG_WARN("Resampler already opened");
        return 0;
    }

    if ((ret = self->plugin->open(self, in_freq, out_freq, frame_duration, in_channels, out_channels, quality, bits_per_sample))) {
        TSK_DEBUG_ERROR("Failed to open [%s] resamplerr", self->plugin->desc);
        return ret;
    }
    else {
        self->opened = tsk_true;
        return 0;
    }
}

tsk_size_t tmedia_resampler_process(tmedia_resampler_t* self, const void* in_data, tsk_size_t in_size_in_sample, void* out_data, tsk_size_t out_size_in_sample)
{
    if (!self || !in_data || !in_size_in_sample || !out_size_in_sample || !self->plugin || !self->plugin->process) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return 0;
    }
    if (!self->opened) {
        TSK_DEBUG_ERROR("Resampler not opened");
        return 0;
    }
    return self->plugin->process(self, in_data, in_size_in_sample, out_data, out_size_in_sample);
}

int tmedia_resampler_close(tmedia_resampler_t* self)
{
    if (!self || !self->plugin) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    if (!self->opened) {
        TSK_DEBUG_WARN("Resampler not opened");
        return 0;
    }

    if (self->plugin->close) {
        int ret;

        if ((ret = self->plugin->close(self))) {
            TSK_DEBUG_ERROR("Failed to close [%s] resamplerr", self->plugin->desc);
            return ret;
        }
        else {
            self->opened = tsk_false;
            return 0;
        }
    }
    else {
        self->opened = tsk_false;
        return 0;
    }
}

int tmedia_resampler_deinit(tmedia_resampler_t* self)
{
    if(!self) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }

    if(self->opened) {
        tmedia_resampler_close(self);
    }

    return 0;
}

int tmedia_resampler_plugin_register(const tmedia_resampler_plugin_def_t* plugin)
{
    if(!plugin) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    if(!__tmedia_resampler_plugin) {
        TSK_DEBUG_INFO("Register resampler: %s", plugin->desc);
        __tmedia_resampler_plugin = plugin;
    }
    return 0;
}

int tmedia_resampler_plugin_unregister(const tmedia_resampler_plugin_def_t* plugin)
{
    (void)(plugin);
    __tmedia_resampler_plugin = tsk_null;
    return 0;
}

tmedia_resampler_t* tmedia_resampler_create()
{
    tmedia_resampler_t* resampler = tsk_null;

    if(__tmedia_resampler_plugin) {
        if((resampler = (tmedia_resampler_t*)tsk_object_new(__tmedia_resampler_plugin->objdef))) {
            resampler->plugin = __tmedia_resampler_plugin;
        }
    }
    return resampler;
}
OpenPOWER on IntegriCloud