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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/*****************************************************************************
* Gnome Wave Cleaner Version 0.19
* Copyright (C) 2001 Jeffrey J. Welty
*
* 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.
*******************************************************************************/
/* amplify.c */
#include <stdlib.h>
#include "gwc.h"
#define BUFSIZE 10000
static gfloat amount_first[2] = {1.0,1.0} ;
static gfloat amount_last[2] = {1.0,1.0} ;
static gfloat amount_first_l[2] = {1.0,0.0} ;
static gfloat amount_last_l[2] = {1.0,0.0} ;
static gfloat amount_first_r[2] = {0.0,1.0} ;
static gfloat amount_last_r[2] = {0.0,1.0} ;
static int feather_width = 20 ;
void simple_amplify_audio(struct sound_prefs *p, long first, long last, int channel_mask, double amount)
{
long left[BUFSIZE], right[BUFSIZE] ;
long current, i ;
int loops = 0 ;
current = first ;
push_status_text("Amplifying audio") ;
update_progress_bar(0.0,PROGRESS_UPDATE_INTERVAL,TRUE) ;
{
while(current <= last) {
long n = MIN(last - current + 1, BUFSIZE) ;
long tmplast = current + n - 1 ;
gfloat p = (gfloat)(current-first)/(last-first+1) ;
n = read_wavefile_data(left, right, current, tmplast) ;
update_progress_bar(p,PROGRESS_UPDATE_INTERVAL,FALSE) ;
for(i = 0 ; i < n ; i++) {
if(channel_mask & 0x01) {
left[i] = lrint(amount*left[i]) ;
}
if(channel_mask & 0x02) {
right[i] = lrint(amount*right[i]) ;
}
}
write_wavefile_data(left, right, current, tmplast) ;
current += n ;
if(last - current < 10) loops++ ;
if(loops > 5) {
warning("infinite loop in amplify_audio, programming error\n") ;
}
}
resample_audio_data(p, first, last) ;
save_sample_block_data(p) ;
}
update_progress_bar(0.0,PROGRESS_UPDATE_INTERVAL,TRUE) ;
pop_status_text() ;
main_redraw(FALSE, TRUE) ;
}
void amplify_audio(struct sound_prefs *p, long first, long last, int channel_mask)
{
long left[BUFSIZE], right[BUFSIZE] ;
long current, i ;
int loops = 0 ;
current = first ;
push_status_text("Amplifying audio") ;
update_progress_bar(0.0,PROGRESS_UPDATE_INTERVAL,TRUE) ;
{
while(current <= last) {
long n = MIN(last - current + 1, BUFSIZE) ;
long tmplast = current + n - 1 ;
gfloat p = (gfloat)(current-first)/(last-first+1) ;
n = read_wavefile_data(left, right, current, tmplast) ;
update_progress_bar(p,PROGRESS_UPDATE_INTERVAL,FALSE) ;
for(i = 0 ; i < n ; i++) {
long icurrent = current + i ;
double p_last = (double)(icurrent-first+1)/(double)(last-first+1) ;
double p_first = 1.0 - p_last ;
double feather_p = 1.0 ;
double wet_left, wet_right ;
if(icurrent - first < feather_width)
feather_p = (double)(icurrent-first)/(double)(feather_width) ;
if(last - icurrent < feather_width)
feather_p = (double)(last - icurrent)/(double)(feather_width) ;
if(channel_mask & 0x01) {
wet_left = ((double)left[i]*(amount_first_l[0]*p_first+amount_last_l[0]*p_last)) ;
wet_left += ((double)right[i]*(amount_first_r[0]*p_first+amount_last_r[0]*p_last)) ;
}
if(channel_mask & 0x02) {
wet_right = ((double)left[i]*(amount_first_l[1]*p_first+amount_last_l[1]*p_last)) ;
wet_right += ((double)right[i]*(amount_first_r[1]*p_first+amount_last_r[1]*p_last)) ;
}
if(channel_mask & 0x01) {
left[i] = lrint((double)left[i]*(1.0-feather_p) + wet_left*feather_p) ;
}
if(channel_mask & 0x02) {
right[i] = lrint((double)right[i]*(1.0-feather_p) + wet_right*feather_p) ;
}
}
write_wavefile_data(left, right, current, tmplast) ;
current += n ;
if(last - current < 10) loops++ ;
if(loops > 5) {
warning("infinite loop in amplify_audio, programming error\n") ;
}
}
resample_audio_data(p, first, last) ;
save_sample_block_data(p) ;
}
update_progress_bar(0.0,PROGRESS_UPDATE_INTERVAL,TRUE) ;
pop_status_text() ;
main_redraw(FALSE, TRUE) ;
}
int amplify_dialog(struct sound_prefs current, struct view *v)
{
GtkWidget *dlg, *maxtext, *dialog_table, *leftframe, *rightframe, *l_tbl, *r_tbl ;
GtkWidget *amount_first_entry_l[2] ;
GtkWidget *amount_last_entry_l[2] ;
GtkWidget *amount_first_entry_r[2] ;
GtkWidget *amount_last_entry_r[2] ;
GtkWidget *feather_width_entry ;
int dclose = 0 ;
int row = 0 ;
int dres ;
char buf[200] ;
/* Alister: everything using gtkcurve has been commented out since gwc-0.20-09 and it appears prior to that */
/* the implementation was never actually completed i.e. the widget worked, but it didn't do anything except */
/* print values to stdout. */
/* GtkWidget *curve ; */
/* gfloat curve_data[20] ; */
dialog_table = gtk_table_new(3,1,0) ;
l_tbl = gtk_table_new(5,2,0) ;
r_tbl = gtk_table_new(5,2,0) ;
gtk_table_set_row_spacings(GTK_TABLE(dialog_table), 12) ;
gtk_table_set_col_spacings(GTK_TABLE(dialog_table), 6) ;
gtk_widget_show (dialog_table);
gtk_table_set_row_spacings(GTK_TABLE(l_tbl), 4) ;
gtk_table_set_col_spacings(GTK_TABLE(l_tbl), 6) ;
gtk_container_set_border_width(GTK_CONTAINER(l_tbl), 5) ;
gtk_widget_show (l_tbl);
gtk_table_set_row_spacings(GTK_TABLE(r_tbl), 4) ;
gtk_table_set_col_spacings(GTK_TABLE(r_tbl), 6) ;
gtk_container_set_border_width(GTK_CONTAINER(r_tbl), 5) ;
gtk_widget_show (r_tbl);
dlg = gtk_dialog_new_with_buttons("Amplify",
GTK_WINDOW(main_window), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK, NULL, NULL);
gtk_dialog_set_default_response (GTK_DIALOG(dlg), GTK_RESPONSE_OK);
sprintf(buf, "Maximum amplification without clipping is %6.2f.\n", (double)1.0/(double)current.max_value) ;
maxtext = gtk_label_new (buf);
gtk_widget_show (maxtext);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dlg)->vbox), maxtext, TRUE, TRUE, 0);
/* curve = gtk_curve_new (); */
/* gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dlg)->vbox), curve, TRUE, TRUE, row++); */
/* gtk_widget_show (curve); */
/* //gtk_curve_set_curve_type(GTK_CURVE(curve),GTK_CURVE_TYPE_LINEAR) ; */
/* gtk_curve_set_range(GTK_CURVE(curve),0.0,100.0,0.0,100.0) ; */
/* gtk_curve_reset(GTK_CURVE(curve)) ; */
/* //gtk_curve_set_vector(GTK_CURVE(curve),2,curve_data) ; */
leftframe = gtk_frame_new ("Left channel source");
gtk_container_add(GTK_CONTAINER(leftframe), l_tbl) ;
gtk_widget_show (leftframe);
gtk_table_attach_defaults(GTK_TABLE(dialog_table), leftframe, 0, 2, row, row+1) ;
row++ ;
amount_first_entry_l[0] = add_number_entry_with_label_double(amount_first_l[0], "Left Channel beginning:", l_tbl, 0) ;
amount_last_entry_l[0] = add_number_entry_with_label_double(amount_last_l[0], "Left Channel end:", l_tbl, 1) ;
amount_first_entry_r[0] = add_number_entry_with_label_double(amount_first_r[0], "Right Channel beginning:", l_tbl, 2) ;
amount_last_entry_r[0] = add_number_entry_with_label_double(amount_last_r[0], "Right Channel end:", l_tbl, 3) ;
rightframe = gtk_frame_new ("Right channel source");
gtk_container_add(GTK_CONTAINER(rightframe), r_tbl) ;
gtk_widget_show (rightframe);
gtk_table_attach_defaults(GTK_TABLE(dialog_table), rightframe, 0, 2, row, row+1) ;
row++ ;
amount_first_entry_l[1] = add_number_entry_with_label_double(amount_first_l[1], "Left Channel beginning:", r_tbl, 0) ;
amount_last_entry_l[1] = add_number_entry_with_label_double(amount_last_l[1], "Left Channel end:", r_tbl, 1) ;
amount_first_entry_r[1] = add_number_entry_with_label_double(amount_first_r[1], "Right Channel beginning:", r_tbl, 2) ;
amount_last_entry_r[1] = add_number_entry_with_label_double(amount_last_r[1], "Right Channel end:", r_tbl, 3) ;
feather_width_entry = add_number_entry_with_label_int(feather_width, "Feather width", dialog_table, row++) ;
gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dlg)->vbox), dialog_table, TRUE, TRUE, 0);
dres = gwc_dialog_run(GTK_DIALOG(dlg)) ;
if(dres == 0) {
int i ;
for(i = 0 ; i < 2 ; i++) {
amount_first_l[i] = atof(gtk_entry_get_text((GtkEntry *)amount_first_entry_l[i])) ;
amount_last_l[i] = atof(gtk_entry_get_text((GtkEntry *)amount_last_entry_l[i])) ;
amount_first_r[i] = atof(gtk_entry_get_text((GtkEntry *)amount_first_entry_r[i])) ;
amount_last_r[i] = atof(gtk_entry_get_text((GtkEntry *)amount_last_entry_r[i])) ;
}
feather_width = atoi(gtk_entry_get_text((GtkEntry *)feather_width_entry)) ;
dclose = 1 ;
}
/* { */
/* int i ; */
/* */
/* gtk_curve_get_vector(GTK_CURVE(curve), 10, curve_data) ; */
/* */
/* for(i = 0 ; i < 10 ; i++) { */
/* printf("%lg %lg\n", curve_data[i*2], curve_data[i*2+1]) ; */
/* } */
/* */
/* } */
gtk_widget_destroy(dlg) ;
if(dres == 0)
return 1 ;
return 0 ;
}
void batch_normalize(struct sound_prefs *p, long first, long last, int channel_mask)
{
amount_first_l[0] = (double)1.0/(double)p->max_value;
amount_last_l[0] = amount_first_l[0] ;
amount_first_r[1] = amount_first_l[0] ;
amount_last_r[1] = amount_first_l[0] ;
feather_width = 2000;
//fprintf(stderr, "Maximum amplification without clipping is %f \n", amount_first_l[0]) ;
amplify_audio(p,first,last,channel_mask);
}
|