summaryrefslogtreecommitdiff
path: root/gst/audiofxbad/gstaudiochannelmix.c
blob: 7d92a5b268dc69664689d0769fcb1ff3d42f6f05 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* GStreamer
 * Copyright (C) 2013 David Schleef <ds@schleef.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
 * Boston, MA 02110-1335, USA.
 */
/**
 * SECTION:element-gstaudiochannelmix
 *
 * The audiochannelmix element mixes channels in stereo audio based on
 * properties set on the element.  The primary purpose is reconstruct
 * equal left/right channels on an input stream that has audio in only
 * one channel.
 *
 * <refsect2>
 * <title>Example launch line</title>
 * |[
 * gst-launch-1.0 -v audiotestsrc ! audiochannelmix ! autoaudiosink
 * ]|
 * </refsect2>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>
#include <gst/audio/gstaudiofilter.h>
#include "gstaudiochannelmix.h"
#include <math.h>

GST_DEBUG_CATEGORY_STATIC (gst_audio_channel_mix_debug_category);
#define GST_CAT_DEFAULT gst_audio_channel_mix_debug_category

/* prototypes */


static void gst_audio_channel_mix_set_property (GObject * object,
    guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_audio_channel_mix_get_property (GObject * object,
    guint property_id, GValue * value, GParamSpec * pspec);

static gboolean gst_audio_channel_mix_setup (GstAudioFilter * filter,
    const GstAudioInfo * info);
static GstFlowReturn gst_audio_channel_mix_transform_ip (GstBaseTransform *
    trans, GstBuffer * buf);

enum
{
  PROP_0,
  PROP_LEFT_TO_LEFT,
  PROP_LEFT_TO_RIGHT,
  PROP_RIGHT_TO_LEFT,
  PROP_RIGHT_TO_RIGHT
};

/* pad templates */

static GstStaticPadTemplate gst_audio_channel_mix_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-raw,format=S16LE,rate=[1,max],"
        "channels=2,layout=interleaved")
    );

static GstStaticPadTemplate gst_audio_channel_mix_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-raw,format=S16LE,rate=[1,max],"
        "channels=2,layout=interleaved")
    );


/* class initialization */

G_DEFINE_TYPE_WITH_CODE (GstAudioChannelMix, gst_audio_channel_mix,
    GST_TYPE_AUDIO_FILTER,
    GST_DEBUG_CATEGORY_INIT (gst_audio_channel_mix_debug_category,
        "audiochannelmix", 0, "debug category for audiochannelmix element"));

static void
gst_audio_channel_mix_class_init (GstAudioChannelMixClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GstBaseTransformClass *base_transform_class =
      GST_BASE_TRANSFORM_CLASS (klass);
  GstAudioFilterClass *audio_filter_class = GST_AUDIO_FILTER_CLASS (klass);

  /* Setting up pads and setting metadata should be moved to
     base_class_init if you intend to subclass this class. */
  gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
      &gst_audio_channel_mix_src_template);
  gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
      &gst_audio_channel_mix_sink_template);

  gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
      "Simple stereo audio mixer", "Audio/Mixer", "Mixes left/right channels "
      "of stereo audio", "David Schleef <ds@schleef.org>");

  gobject_class->set_property = gst_audio_channel_mix_set_property;
  gobject_class->get_property = gst_audio_channel_mix_get_property;
  audio_filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_channel_mix_setup);
  base_transform_class->transform_ip =
      GST_DEBUG_FUNCPTR (gst_audio_channel_mix_transform_ip);

  g_object_class_install_property (gobject_class, PROP_LEFT_TO_LEFT,
      g_param_spec_double ("left-to-left", "Left to Left",
          "Left channel to left channel gain",
          -G_MAXDOUBLE, G_MAXDOUBLE, 1.0,
          GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class, PROP_LEFT_TO_RIGHT,
      g_param_spec_double ("left-to-right", "Left to Right",
          "Left channel to right channel gain",
          -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
          GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class, PROP_RIGHT_TO_LEFT,
      g_param_spec_double ("right-to-left", "Right to Left",
          "Right channel to left channel gain",
          -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
          GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class, PROP_RIGHT_TO_RIGHT,
      g_param_spec_double ("right-to-right", "Right to Right",
          "Right channel to right channel gain",
          -G_MAXDOUBLE, G_MAXDOUBLE, 1.0,
          GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}

static void
gst_audio_channel_mix_init (GstAudioChannelMix * audiochannelmix)
{
  audiochannelmix->left_to_left = 1.0;
  audiochannelmix->left_to_right = 0.0;
  audiochannelmix->right_to_left = 0.0;
  audiochannelmix->right_to_right = 1.0;
}

void
gst_audio_channel_mix_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec)
{
  GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (object);

  GST_DEBUG_OBJECT (audiochannelmix, "set_property");

  switch (property_id) {
    case PROP_LEFT_TO_LEFT:
      audiochannelmix->left_to_left = g_value_get_double (value);
      break;
    case PROP_LEFT_TO_RIGHT:
      audiochannelmix->left_to_right = g_value_get_double (value);
      break;
    case PROP_RIGHT_TO_LEFT:
      audiochannelmix->right_to_left = g_value_get_double (value);
      break;
    case PROP_RIGHT_TO_RIGHT:
      audiochannelmix->right_to_right = g_value_get_double (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}

void
gst_audio_channel_mix_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec)
{
  GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (object);

  GST_DEBUG_OBJECT (audiochannelmix, "get_property");

  switch (property_id) {
    case PROP_LEFT_TO_LEFT:
      g_value_set_double (value, audiochannelmix->left_to_left);
      break;
    case PROP_LEFT_TO_RIGHT:
      g_value_set_double (value, audiochannelmix->left_to_right);
      break;
    case PROP_RIGHT_TO_LEFT:
      g_value_set_double (value, audiochannelmix->right_to_left);
      break;
    case PROP_RIGHT_TO_RIGHT:
      g_value_set_double (value, audiochannelmix->right_to_right);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}

static gboolean
gst_audio_channel_mix_setup (GstAudioFilter * filter, const GstAudioInfo * info)
{
#ifndef GST_DISABLE_GST_DEBUG
  GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (filter);

  GST_DEBUG_OBJECT (audiochannelmix, "setup");
#endif

  return TRUE;
}

static GstFlowReturn
gst_audio_channel_mix_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
{
  GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (trans);
  int n;
  GstMapInfo map;
  int i;
  double ll = audiochannelmix->left_to_left;
  double lr = audiochannelmix->left_to_right;
  double rl = audiochannelmix->right_to_left;
  double rr = audiochannelmix->right_to_right;
  int l, r;
  gint16 *data;

  GST_DEBUG_OBJECT (audiochannelmix, "transform_ip");

  gst_buffer_map (buf, &map, GST_MAP_WRITE | GST_MAP_READ);

  n = gst_buffer_get_size (buf) >> 2;
  data = (gint16 *) map.data;
  for (i = 0; i < n; i++) {
    l = data[2 * i + 0];
    r = data[2 * i + 1];
    data[2 * i + 0] = CLAMP (rint (ll * l + rl * r), -32768, 32767);
    data[2 * i + 1] = CLAMP (rint (lr * l + rr * r), -32768, 32767);
  }

  gst_buffer_unmap (buf, &map);

  return GST_FLOW_OK;
}