File: fade.c

package info (click to toggle)
sweep 0.9.0-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,100 kB
  • ctags: 3,518
  • sloc: ansic: 37,436; sh: 9,756; makefile: 664; yacc: 288; sed: 16
file content (203 lines) | stat: -rw-r--r-- 4,767 bytes parent folder | download | duplicates (3)
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
/*
 * Sweep, a sound wave editor.
 *
 * Copyright (C) 2000 Conrad Parker
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <string.h>

#include <gdk/gdkkeysyms.h>

#include <sweep/sweep.h>

#include <../src/sweep_app.h> /* XXX */

static sw_sample *
fade (sw_sample * sample, gfloat start, gfloat end)
{
  sw_sounddata * sounddata;
  sw_format * f;
  GList * gl;
  sw_sel * sel;
  sw_audio_t * d;
  gfloat factor = start;
  sw_framecount_t op_total, run_total;
  glong i;
  sw_framecount_t offset, remaining, n;

  gboolean active = TRUE;

  sounddata = sample_get_sounddata (sample);
  f = sounddata->format;

  op_total = sounddata_selection_nr_frames (sounddata) / 100;
  if (op_total == 0) op_total = 1;
  run_total = 0;

#if 0
  /* Find max */
  for (gl = sounddata->sels; active && gl; gl = gl->next) {
    sel = (sw_sel *)gl->data;

    offset = 0;
    remaining = sel->sel_end - sel->sel_start;

    while (active && remaining > 0) {
      g_mutex_lock (sample->ops_mutex);

      if (sample->edit_state == SWEEP_EDIT_STATE_CANCEL) {
	active = FALSE;
      } else {

	d = sounddata->data + frames_to_bytes (f, sel->sel_start + offset);

	n = MIN(remaining, 1024);

	for (i=0; i < n * f->channels; i++) {
	  if(d[i]>=0) max = MAX(max, d[i]);
	  else max = MAX(max, -d[i]);
	}

	remaining -= n;
	offset += n;

	run_total += n;
	sample_set_progress_percent (sample, run_total / op_total);
      }

      g_mutex_unlock (sample->ops_mutex);
    }
  }

  if (max != 0) factor = SW_AUDIO_T_MAX / (gfloat)max;
#endif

  /* Fade */
  for (gl = sounddata->sels; active && gl; gl = gl->next) {
    sel = (sw_sel *)gl->data;

    offset = 0;
    remaining = sel->sel_end - sel->sel_start;

    while (active && remaining > 0) {
      g_mutex_lock (sample->ops_mutex);

      if (sample->edit_state == SWEEP_EDIT_STATE_CANCEL) {
	active = FALSE;
      } else {
	d = sounddata->data + frames_to_bytes (f, sel->sel_start + offset);

	n = MIN(remaining, 1024);

	factor = start + (end - start) * 0.01 *
	  (gfloat)run_total / (gfloat)op_total;

	for (i=0; i < n * f->channels; i++) {
	  d[i] = (sw_audio_t)((gfloat)d[i] * factor);
	}

	remaining -= n;
	offset += n;

	run_total += n;
	sample_set_progress_percent (sample, run_total * 100 / op_total);
      }

      g_mutex_unlock (sample->ops_mutex);
    }
  }

  return sample;
}

static sw_sample *
fade_in (sw_sample * sample, sw_param_set pset, gpointer custom_data)
{
  return fade (sample, 0.0, 1.0);
}

static sw_sample *
fade_out (sw_sample * sample, sw_param_set pset, gpointer custom_data)
{
  return fade (sample, 1.0, 0.0);
}

static sw_op_instance *
apply_fade_in (sw_sample * sample, sw_param_set pset, gpointer custom_data)
{
  return
    perform_filter_op (sample, _("Fade in"), (SweepFilter)fade_in,
		       pset, NULL);
}

static sw_op_instance *
apply_fade_out (sw_sample * sample, sw_param_set pset, gpointer custom_data)
{
  return
    perform_filter_op (sample, _("Fade out"), (SweepFilter)fade_out,
		       pset, NULL);
}

static sw_procedure proc_fade_in = {
  N_("Fade in"),
  N_("Apply a linear fade to the selection, fading in from silence"),
  "Conrad Parker",
  "Copyright (C) 2002",
  "http://sweep.sourceforge.net/plugins/fade",
  "Filters/Fade_In", /* identifier */
  0, /* accel_key */
  0, /* accel_mods */
  0, /* nr_params */
  NULL, /* param_specs */
  NULL, /* suggests() */
  apply_fade_in,
  NULL, /* custom_data */
};

static sw_procedure proc_fade_out = {
  N_("Fade out"),
  N_("Apply a linear fade to the selection, fading out to silence"),
  "Conrad Parker",
  "Copyright (C) 2002",
  "http://sweep.sourceforge.net/plugins/fade",
  "Filters/Fade_In", /* identifier */
  0, /* accel_key */
  0, /* accel_mods */
  0, /* nr_params */
  NULL, /* param_specs */
  NULL, /* suggests() */
  apply_fade_out,
  NULL, /* custom_data */
};
  
static GList *
fade_init (void)
{
  GList * gl = NULL;

  gl = g_list_append (gl, &proc_fade_in);
  gl = g_list_append (gl, &proc_fade_out);

  return gl;
}


sw_plugin plugin = {
  fade_init, /* plugin_init */
  NULL, /* plugin_cleanup */
};