-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaudio_util.c
1434 lines (1140 loc) · 36.3 KB
/
audio_util.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* 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.
*******************************************************************************/
/* audio_util.c */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <memory.h>
#ifdef MAC_OS_X
/* this seems to give wrong results on intel macs :( */
#include <machine/endian.h>
/* doing this doesn't seem to fix it, and would presumably break it on */
/* powerpc macs (but are we otherwise supported there?) */
/*#define __BYTE_ORDER __LITTLE_ENDIAN */
#else
#include <endian.h>
#endif
#include <fcntl.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include <sndfile.h>
#ifdef HAVE_OGG
#include "vorbis/codec.h"
#include "vorbis/vorbisfile.h"
#endif
#include "gwc.h"
#ifdef HAVE_MP3
#include "mpg123.h"
#endif
#include "fmtheaders.h"
#include "encoding.h"
#include "audio_device.h"
#include "soundfile.h"
int audio_state = AUDIO_IS_IDLE ;
int wavefile_fd = -1 ;
long audio_bytes_written ;
int rate = 44100 ;
int stereo = 1 ;
int audio_bits = 16 ;
int BYTESPERSAMPLE = 2 ;
int MAXSAMPLEVALUE = 1 ;
int PLAYBACK_FRAMESIZE = 4 ;
int FRAMESIZE = 4 ;
int current_ogg_bitstream = 0 ;
int nonzero_seek ;
/* int dump_sample = 0 ; */
long wavefile_data_start ;
SNDFILE *sndfile = NULL ;
SF_INFO sfinfo ;
#ifdef HAVE_OGG
OggVorbis_File oggfile ;
#endif
FILE *fp_ogg = NULL ;
#ifdef HAVE_MP3
mpg123_handle *fp_mp3 = NULL ;
#endif
int audiofileisopen = 0 ;
long current_ogg_or_mp3_pos ;
#define SNDFILE_TYPE 0x01
#define OGG_TYPE 0x02
#define MP3_TYPE 0x04
int audio_type ;
extern struct view audio_view ;
extern struct sound_prefs prefs ;
extern struct encoding_prefs encoding_prefs;
int current_sample ;
void position_wavefile_pointer(long sample_number) ;
void audio_normalize(int flag)
{
if(audio_type == SNDFILE_TYPE) {
if(flag == 0)
sf_command(sndfile, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
else
sf_command(sndfile, SFC_SET_NORM_DOUBLE, NULL, SF_TRUE) ;
}
}
void write_wav_header(int thefd, int speed, long bcount, int bits, int stereo)
{
/* Spit out header here... */
wavhead header;
char *riff = "RIFF";
char *wave = "WAVE";
char *fmt = "fmt ";
char *data = "data";
memcpy(&(header.main_chunk), riff, 4);
header.length = sizeof(wavhead) - 8 + bcount;
memcpy(&(header.chunk_type), wave, 4);
memcpy(&(header.sub_chunk), fmt, 4);
header.sc_len = 16;
header.format = 1;
header.modus = stereo + 1;
header.sample_fq = speed;
header.byte_p_sec = ((bits > 8)? 2:1)*(stereo+1)*speed;
/* Correction by J.A. Bezemer: */
header.byte_p_spl = ((bits > 8)? 2:1)*(stereo+1);
/* was: header.byte_p_spl = (bits > 8)? 2:1; */
header.bit_p_spl = bits;
memcpy(&(header.data_chunk), data, 4);
header.data_length = bcount;
write(thefd, &header, sizeof(header));
}
void config_audio_device(int rate_set, int bits_set, int stereo_set)
{
AUDIO_FORMAT format,format_set;
int channels ;
/* int fragset = 0x7FFF000F ; */
bits_set = 16 ;
/* play everything as 16 bit, signed integers */
/* using the appropriate endianness */
/* Alister: I have swapped this around as an intel mac seems to think */
/* the first test is true regardless of whether you test for BE or LE */
/* Presumably it might fail on a powerpc mac now? */
/* Also, does it break other platforms (since LE is normal these days */
/* it should really stay default */
#if __BYTE_ORDER == __LITTLE_ENDIAN
format_set = GWC_S16_LE ;
#elif __BYTE_ORDER == __BIG_ENDIAN
format_set = GWC_S16_BE ;
#else
format_set = GWC_S16_LE ;
#endif
rate = rate_set ;
audio_bits = bits_set ;
stereo = stereo_set ;
format = format_set ;
/* if(ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &fragset) == -1) { */
/* warning("error setting buffer size on audio device") ; */
/* } */
channels = stereo + 1 ;
// Alister: Eh? Isn't this set above?
rate = rate_set ;
if (audio_device_set_params(&format_set, &channels, &rate) == -1) {
warning("unknown error setting device parameter") ;
}
if(format != format_set) {
char *buf_fmt_str ;
char buf[85] ;
switch(format_set) {
case GWC_U8 : buf_fmt_str = "8 bit (unsigned)" ; bits_set = 8 ; break ;
case GWC_S8 : buf_fmt_str = "8 bit (signed)" ; bits_set = 8 ; break ;
case GWC_S16_BE :
case GWC_S16_LE : buf_fmt_str = "16 bit" ; bits_set = 16 ; break ;
default : buf_fmt_str = "unknown!" ; bits_set = 8 ; break ;
}
snprintf(buf, sizeof(buf), "Set bits to %s - does your soundcard support what you requested?\n", buf_fmt_str) ;
warning(buf) ;
}
if(channels != stereo + 1) {
char buf[80] ;
if(stereo == 0)
snprintf(buf, sizeof(buf), "Failed to set mono mode\nYour sound card may not support mono\n") ;
else
snprintf(buf, sizeof(buf), "Failed to set stereo mode\nYour sound card may not support stereo\n") ;
warning(buf) ;
}
stereo_set = channels - 1 ;
// Alister: eh? does this make sense if rate has been set from rate_set above?
if(ABS(rate_set-rate) > 10) {
char buf[80] ;
snprintf(buf, sizeof(buf), "Rate set to %d instead of %d\nYour sound card may not support the desired rate\n",
rate_set, rate) ;
warning(buf) ;
}
// Alister: Eh? Isn't this set above?
rate = rate_set ;
audio_bits = bits_set ;
stereo = stereo_set ;
}
long playback_samples_remaining = 0 ;
long playback_total_bytes ;
int playback_bytes_per_block ;
int looped_count ;
#define MAXBUFSIZE 32768
int BUFSIZE ;
unsigned char audio_buffer[MAXBUFSIZE] ;
unsigned char audio_buffer2[MAXBUFSIZE] ;
long playback_start_position ;
long playback_end_position ;
long playback_position ;
long first_playback_sample ;
long set_playback_cursor_position(struct view *v, long millisec_per_visual_frame)
{
long first, last ;
if(audio_state == AUDIO_IS_PLAYBACK) {
long bytes = audio_device_processed_bytes()-looped_count*playback_total_bytes ;
get_region_of_interest(&first, &last, v) ;
v->cursor_position = first_playback_sample+bytes/(PLAYBACK_FRAMESIZE) ;
return playback_total_bytes - bytes ;
}
{
long inc = rate*millisec_per_visual_frame/1000 ;
/* g_print("inc:%ld\n", inc) ; */
v->cursor_position += inc ;
return 1 ;
}
}
long start_playback(char *output_device, struct view *v, struct sound_prefs *p, double seconds_per_block, double seconds_to_preload)
{
long first, last ;
long playback_samples ;
gfloat lv, rv ;
if(audio_type == SNDFILE_TYPE && sndfile == NULL) return 1 ;
#ifdef HAVE_OGG
if(audio_type == OGG_TYPE && fp_ogg == NULL) return 1 ;
#endif
audio_device_close(1) ;
if (audio_device_open(output_device) == -1) {
char buf[255] ;
snprintf(buf, sizeof(buf), "Failed to open OSS audio output device %s, check settings->miscellaneous for device information", output_device) ;
#ifdef HAVE_ALSA
snprintf(buf, sizeof(buf), "Failed to open alsa output device %s, check settings->miscellaneous for device information", output_device) ;
#endif
#ifdef HAVE_PULSE_AUDIO
snprintf(buf, sizeof(buf), "Failed to open Pulse audio output device, recommend internet search about pulse audio configuration for your OS") ;
#endif
warning(buf) ;
return 0 ;
}
get_region_of_interest(&first, &last, v) ;
/* g_print("first is %ld\n", first) ; */
/* g_print("last is %ld\n", last) ; */
/* g_print("rate is %ld\n", (long)p->rate) ; */
first_playback_sample = first ;
playback_start_position = first ;
playback_end_position = last+1;
playback_position = playback_start_position ;
playback_samples = p->rate*seconds_per_block ;
playback_bytes_per_block = playback_samples*PLAYBACK_FRAMESIZE ;
// This was moved down 8 lines to make it work in OS X. Rob
config_audio_device(p->rate, p->playback_bits, p->stereo); //Set up the audio device.
//stereo is 1 if it is stereo
//playback_bits is the number of bits per sample
//rate is the number of samples per second
BUFSIZE = audio_device_best_buffer_size(playback_bytes_per_block);
playback_bytes_per_block = BUFSIZE ;
if(playback_bytes_per_block > MAXBUFSIZE) {
playback_bytes_per_block = MAXBUFSIZE ;
}
playback_samples = playback_bytes_per_block/PLAYBACK_FRAMESIZE ;
BUFSIZE = playback_bytes_per_block ;
playback_samples_remaining = (last-first+1) ;
playback_total_bytes = playback_samples_remaining*PLAYBACK_FRAMESIZE ;
audio_state = AUDIO_IS_PLAYBACK ;
position_wavefile_pointer(playback_start_position) ;
/* g_print("playback_start_position is %ld\n", playback_start_position) ; */
/* put some data in the buffer queues, to avoid underflows */
if(0) {
int n = (int)(seconds_to_preload / seconds_per_block+0.5) ;
int old_playback_bytes = playback_bytes_per_block ;
playback_bytes_per_block *= n ;
if(playback_bytes_per_block > MAXBUFSIZE) playback_bytes_per_block = MAXBUFSIZE ;
process_audio(&lv, &rv) ;
v->cursor_position = first+playback_bytes_per_block/(PLAYBACK_FRAMESIZE) ;
playback_bytes_per_block = old_playback_bytes ;
}
/* g_print("playback_samples is %ld\n", playback_samples) ; */
/* g_print("BUFSIZE %ld (%lg fragments)\n", (long)BUFSIZE, (double)BUFSIZE/(double)oss_info.fragsize) ; */
v->prev_cursor_position = -1 ;
looped_count = 0 ;
return playback_samples ;
}
void *wavefile_data ;
#ifdef TRUNCATE_OLD
void truncate_wavfile(struct view *v)
{
#define REALLY_TRUNCATE
#ifndef REALLY_TRUNCATE
warning("Truncation temporarily disabled, while incorporating libsndfile...") ;
#else
/* we must do 3 things:
1. Shift all samples forward by v->truncate_head
2. Rescan the sample blocks (along the way)
3. Physically truncate the size of the file on
the filesystem by (v->truncate_head + (n_samples-1)-v->truncate_tail) samples
*/
long prev ;
long new ;
int n_in_buf ;
long first, last ;
#define TMPBUFSIZE (SBW*1000)
long left[TMPBUFSIZE], right[TMPBUFSIZE] ;
push_status_text("Truncating audio data") ;
update_progress_bar(0.0, PROGRESS_UPDATE_INTERVAL, TRUE) ;
/* something like this, gotta buffer this or the disk head will
burn a hole in the platter */
if(v->truncate_head > 0) {
for(prev = v->truncate_head ; prev <= v->truncate_tail ; prev += TMPBUFSIZE) {
update_progress_bar((gfloat)(prev-v->truncate_head)/(gfloat)(v->truncate_tail-v->truncate_head), PROGRESS_UPDATE_INTERVAL, FALSE) ;
last = MIN((prev+TMPBUFSIZE-1), v->truncate_tail) ;
n_in_buf = read_wavefile_data(left, right, prev, last) ;
new = prev - v->truncate_head ;
first = new ;
last = new + n_in_buf - 1 ;
write_wavefile_data(left, right, first, last) ;
resample_audio_data(&prefs, first, last) ;
}
}
prefs.n_samples = v->truncate_tail - v->truncate_head + 1 ;
if(1) save_sample_block_data(&prefs) ;
if(1) {
sf_count_t total_samples = prefs.n_samples ;
if(sf_command(sndfile, SFC_FILE_TRUNCATE, &total_samples, sizeof(total_samples)))
warning("Libsndfile reports truncation of audio file failed") ;
}
pop_status_text() ;
#endif
}
#endif /* !TRUNCATE_OLD */
void sndfile_truncate(long n_samples)
{
sf_count_t total_samples = n_samples ;
if(sf_command(sndfile, SFC_FILE_TRUNCATE, &total_samples, sizeof(total_samples)))
warning("Libsndfile reports truncation of audio file failed") ;
}
int close_wavefile(struct view *v)
{
if(audio_type == SNDFILE_TYPE) {
#ifdef TRUNCATE_OLD
int r ;
if(v->truncate_head > 0 || v->truncate_tail < v->n_samples -1) {
r = yesnocancel("Part of the waveform is selected for truncation, do you really want to truncate?") ;
if(r == 2) return 0 ;
if(r == 0) truncate_wavfile(v) ;
}
#endif /* TRUNCATE_OLD */
if(sndfile != NULL) {
sf_close(sndfile) ;
}
audio_device_close(0) ;
sndfile = NULL ;
#ifdef HAVE_OGG
} else if(audio_type == OGG_TYPE) {
if(fp_ogg != NULL) {
ov_clear(&oggfile) ;
}
fp_ogg = NULL ;
#endif
#ifdef HAVE_MP3
} else if(audio_type == MP3_TYPE) {
if(fp_mp3 != NULL) {
mpg123_close(fp_mp3) ;
}
fp_mp3 = NULL ;
#endif
}
return 1 ;
}
void save_as_wavfile(char *filename_new, long first_sample, long last_sample)
{
SNDFILE *sndfile_new ;
SF_INFO sfinfo_new ;
long total_samples ;
long total_bytes ;
total_samples = last_sample-first_sample+1 ;
if(total_samples < 0) {
warning("Invalid selection") ;
return ;
}
total_bytes = total_samples*FRAMESIZE ;
sfinfo_new = sfinfo ;
sfinfo_new.frames = total_samples ;
if (! (sndfile_new = sf_open (filename_new, SFM_WRITE, &sfinfo_new))) {
/* Open failed so print an error message. */
char buf[PATH_MAX] ;
snprintf(buf, sizeof(buf), "Cannot write to %s: %s", filename_new, strerror(errno)) ;
warning(buf) ;
return ;
} ;
push_status_text("Saving selection") ;
/* something like this, gotta buffer this or the disk head will
burn a hole in the platter */
position_wavefile_pointer(first_sample) ;
{
long n_copied ;
#define TMPBUFSIZE (SBW*1000)
unsigned char buf[TMPBUFSIZE] ;
long framebufsize = (TMPBUFSIZE/FRAMESIZE) * FRAMESIZE ;
update_progress_bar(0.0,PROGRESS_UPDATE_INTERVAL,TRUE) ;
for(n_copied = 0 ; n_copied < total_bytes ; n_copied += framebufsize) {
long n_to_copy = framebufsize ;
#ifdef MAC_OS_X /* MacOSX */
usleep(2) ; // prevents segfault on OSX, who knows, something to do with status bar update...
#endif
update_progress_bar((gfloat)(n_copied)/(gfloat)(total_bytes),PROGRESS_UPDATE_INTERVAL,FALSE) ;
if(n_copied + n_to_copy > total_bytes) n_to_copy = total_bytes - n_copied ;
n_to_copy = sf_read_raw(sndfile, buf, n_to_copy) ;
sf_write_raw(sndfile_new, buf, n_to_copy) ;
}
}
update_progress_bar((gfloat)0.0,PROGRESS_UPDATE_INTERVAL,TRUE) ;
sf_close(sndfile_new) ;
pop_status_text() ;
}
void save_selection_as_wavfile(char *filename_new, struct view *v)
{
SNDFILE *sndfile_new ;
SF_INFO sfinfo_new ;
long total_samples ;
long total_bytes ;
total_samples = v->selected_last_sample-v->selected_first_sample+1 ;
if(total_samples < 0 || total_samples > v->n_samples) {
warning("Invalid selection") ;
return ;
}
save_as_wavfile(filename_new, v->selected_first_sample, v->selected_last_sample) ;
}
#ifdef HAVE_MP3
int gwc_mpg123_open(char *filename)
{
int r ;
mpg123_init() ;
fp_mp3 = mpg123_new(NULL,NULL) ;
r = mpg123_open(fp_mp3,filename) ;
if(r != MPG123_OK) {
mpg123_delete(fp_mp3) ;
fp_mp3 = NULL ;
}
return r ;
}
int gwc_mpg123_close(void)
{
mpg123_close(fp_mp3) ;
mpg123_delete(fp_mp3) ;
mpg123_exit() ;
return 0 ;
}
#endif
int is_valid_audio_file(char *filename)
{
SNDFILE *sndfile ;
SF_INFO sfinfo ;
sfinfo.format = 0 ;
#ifdef HAVE_OGG
if((fp_ogg = fopen(filename, "r")) != NULL) {
if(ov_open(fp_ogg, &oggfile, NULL, 0) < 0) {
fclose(fp_ogg) ;
fp_ogg = NULL ;
} else {
ov_clear(&oggfile) ;
fclose(fp_ogg) ;
fp_ogg = NULL ;
audio_type = OGG_TYPE ;
return 1 ;
}
}
#endif
#ifdef HAVE_MP3
if(gwc_mpg123_open(filename) == MPG123_OK) {
gwc_mpg123_close() ;
fp_mp3 = NULL ;
audio_type = MP3_TYPE ;
return 1 ;
}
#endif
if((sndfile = sf_open(filename, SFM_RDWR, &sfinfo)) != NULL) {
sf_close(sndfile) ;
audio_type = SNDFILE_TYPE ;
return 1 ;
} else {
char buf[180+PATH_MAX] ;
snprintf(buf, sizeof(buf), "Failed to open %s, \'%s\'", filename, sf_strerror(NULL)) ;
warning(buf) ;
}
return 0 ;
}
struct sound_prefs open_wavefile(char *filename, struct view *v)
{
struct sound_prefs wfh ;
/* initialize all wfh structure members to defaults. Will be overwritten on successful file open */
wfh.rate = 44100 ;
wfh.n_channels = 2 ;
wfh.stereo = 1 ;
wfh.n_samples = 2 ;
wfh.playback_bits = wfh.bits = 16 ;
wfh.max_allowed = MAXSAMPLEVALUE-1 ;
wfh.wavefile_fd = wavefile_fd ;
wfh.sample_buffer_exists = FALSE ;
if(close_wavefile(v)) {
wfh.successful_open = TRUE ;
} else {
wfh.successful_open = FALSE ;
return wfh ;
}
if(audio_type == SNDFILE_TYPE) {
if (! (sndfile = sf_open (filename, SFM_RDWR, &sfinfo))) {
/* Open failed so print an error message. */
char buf[80+PATH_MAX] ;
snprintf(buf, sizeof(buf), "Failed to open %s, no permissions or unknown audio format", filename) ;
warning(buf) ;
wfh.successful_open = FALSE ;
return wfh ;
/* Print the error message from libsndfile. */
/* sf_perror (NULL) ; */
/* return 1 ; */
} ;
}
#ifdef HAVE_OGG
if(audio_type == OGG_TYPE) {
if((fp_ogg = fopen(filename, "r")) != NULL) {
if(ov_open(fp_ogg, &oggfile, NULL, 0) < 0) {
/* Open failed so print an error message. */
char buf[80+PATH_MAX] ;
snprintf(buf, sizeof(buf), "Failed to open %s", filename) ;
warning(buf) ;
wfh.successful_open = FALSE ;
fclose(fp_ogg) ;
fp_ogg = NULL ;
return wfh ;
}
}
}
#endif
#ifdef HAVE_MP3
if(audio_type == MP3_TYPE) {
if(gwc_mpg123_open(filename) != MPG123_OK) {
/* Open failed so print an error message. */
char buf[80+PATH_MAX] ;
snprintf(buf, sizeof(buf), "Failed to open %s", filename) ;
warning(buf) ;
wfh.successful_open = FALSE ;
fp_mp3 = NULL ;
return wfh ;
}
}
#endif
wfh.wavefile_fd = 1 ;
if(audio_type == SNDFILE_TYPE) {
/* determine soundfile properties */
wfh.rate = sfinfo.samplerate ;
wfh.n_channels = sfinfo.channels ;
wfh.stereo = stereo = sfinfo.channels-1 ;
wfh.n_samples = sfinfo.frames ;
switch(sfinfo.format & 0x00000F) {
case SF_FORMAT_PCM_U8 : BYTESPERSAMPLE=1 ; MAXSAMPLEVALUE = 1 << 8 ; break ;
case SF_FORMAT_PCM_S8 : BYTESPERSAMPLE=1 ; MAXSAMPLEVALUE = 1 << 7 ; break ;
case SF_FORMAT_PCM_16 : BYTESPERSAMPLE=2 ; MAXSAMPLEVALUE = 1 << 15 ; break ;
case SF_FORMAT_PCM_24 : BYTESPERSAMPLE=3 ; MAXSAMPLEVALUE = 1 << 23 ; break ;
case SF_FORMAT_PCM_32 : BYTESPERSAMPLE=4 ; MAXSAMPLEVALUE = 1 << 31 ; break ;
default : warning("Soundfile format not allowed") ; break ;
}
/* do some simple error checking on the wavfile header , so we don't seek data where it isn't */
if(wfh.n_samples < 2) {
char tmp[140] ;
snprintf(tmp, sizeof(tmp), "Audio file is possibly corrupt, only %ld samples reported by audio header", wfh.n_samples) ;
info(tmp) ;
if(sndfile != NULL) {
sf_close(sndfile) ;
}
wfh.successful_open = FALSE ;
return wfh ;
}
}
#ifdef HAVE_OGG
if(audio_type == OGG_TYPE) {
vorbis_info *vi = ov_info(&oggfile,-1) ;
wfh.rate = vi->rate ;
wfh.n_channels = vi->channels ;
wfh.stereo = stereo = vi->channels-1 ;
wfh.n_samples = ov_pcm_total(&oggfile,-1) ;
BYTESPERSAMPLE=2 ;
MAXSAMPLEVALUE = 1 << 15 ;
current_ogg_or_mp3_pos = 0 ;
fprintf(stderr, "Oggfile: FRAMESIZE=%d\n", BYTESPERSAMPLE*wfh.n_channels) ;
wfh.successful_open = TRUE ;
}
#endif
#ifdef HAVE_MP3
if(audio_type == MP3_TYPE) {
long rate ;
int channels ;
int encoding ;
mpg123_getformat(fp_mp3,&rate,&channels,&encoding) ;
mpg123_scan(fp_mp3) ;
wfh.n_samples = mpg123_length(fp_mp3) ;
wfh.rate = rate ;
wfh.n_channels = channels ;
wfh.stereo = stereo = channels-1 ;
BYTESPERSAMPLE=2 ;
MAXSAMPLEVALUE = 1 << 15 ;
off_t pos = mpg123_tell(fp_mp3) ;
off_t curr_frame = mpg123_tellframe(fp_mp3) ;
current_ogg_or_mp3_pos = 0 ;
fprintf(stderr, "Mp3file: FRAMESIZE=%d, pos=%d, frame=%d\n", BYTESPERSAMPLE*wfh.n_channels, (int)pos, (int)curr_frame) ;
wfh.successful_open = TRUE ;
}
#endif
FRAMESIZE = BYTESPERSAMPLE*wfh.n_channels ;
PLAYBACK_FRAMESIZE = 2*wfh.n_channels ;
wfh.playback_bits = audio_bits = wfh.bits = BYTESPERSAMPLE*8 ;
wfh.max_allowed = MAXSAMPLEVALUE-1 ;
gwc_window_set_title(filename) ;
return wfh ;
}
void position_wavefile_pointer(long sample_number)
{
if(audio_type == SNDFILE_TYPE) {
sf_seek(sndfile, sample_number, SEEK_SET) ;
} else if(audio_type == MP3_TYPE) {
#ifdef HAVE_MP3
if(current_ogg_or_mp3_pos != sample_number) {
off_t new_pos ;
current_ogg_or_mp3_pos = sample_number ;
if(sample_number != 0) {
unsigned char buf[1152*4] ;
new_pos = mpg123_seek(fp_mp3,sample_number,SEEK_SET) ;
off_t curr_frame = mpg123_tellframe(fp_mp3) ;
/* if(curr_frame > 0) curr_frame-- ; */
mpg123_seek_frame(fp_mp3,curr_frame,SEEK_SET) ;
int presample_number = (int)mpg123_tell(fp_mp3) ;
/* if(presample_number > 0) presample_number-- ; */
int samples_to_read = sample_number - presample_number ;
new_pos = mpg123_seek(fp_mp3,presample_number,SEEK_SET) ;
/* fprintf(stderr, "position_wf_ptr, samples_to_read:%d > 1152!!\n", samples_to_read) ; */
/* fprintf(stderr, "curr_frame:%d presample_number:%d\n", curr_frame,presample_number) ; */
/* fprintf(stderr, "position_wf_ptr, want:%d got%d\n", (int)sample_number, (int)new_pos) ; */
if(samples_to_read > 1152) {
exit(1) ;
}
unsigned int done ;
int err ;
err = mpg123_read(fp_mp3, buf, samples_to_read*FRAMESIZE, &done) ;
} else {
new_pos = mpg123_seek(fp_mp3,sample_number,SEEK_SET) ;
nonzero_seek = 0 ;
}
/* fprintf(stderr, "position_wf_ptr, want:%d got%d\n", (int)sample_number, (int)new_pos) ; */
}
#endif
} else {
#ifdef HAVE_OGG
if(current_ogg_or_mp3_pos != sample_number) {
fprintf(stderr, "pos_wv_ptr, was %ld, want %ld\n", current_ogg_or_mp3_pos, sample_number) ;
ov_pcm_seek(&oggfile, sample_number) ;
current_ogg_or_mp3_pos = sample_number ;
}
#endif
}
}
int read_raw_wavefile_data(char buf[], long first, long last)
{
long n = last - first + 1 ;
int n_read = 0 ;
int n_bytes_read = 0 ;
int bufsize = n * FRAMESIZE ;
position_wavefile_pointer(first) ;
if(audio_type == SNDFILE_TYPE) {
n_bytes_read = sf_read_raw(sndfile, buf, n*FRAMESIZE) ;
return n_bytes_read/FRAMESIZE ;
}
#ifdef HAVE_OGG
if(audio_type == OGG_TYPE) {
int ret ;
while(n_read < n) {
ret = ov_read(&oggfile, (char *)&buf[n_bytes_read], bufsize-n_bytes_read,0,2,1,¤t_ogg_bitstream) ;
if(ret > 0) {
n_read += ret/FRAMESIZE ;
n_bytes_read += ret ;
} else {
break ;
}
}
current_ogg_or_mp3_pos += n_read ;
return n_read ;
}
#endif
#ifdef HAVE_MP3
if(audio_type == MP3_TYPE) {
size_t done ;
int err ;
struct mpg123_frameinfo mi ;
while(n_read < n) {
err = mpg123_read(fp_mp3, (unsigned char *)&buf[n_bytes_read], bufsize-n_bytes_read, &done) ;
if(err != MPG123_OK) fprintf(stderr, "read had a problem, %d\n", err) ;
err = mpg123_info(fp_mp3, &mi) ;
/* fprintf(stderr, "fs %d\n", (int)mi.framesize) ; */
n_bytes_read += done ;
n_read += done/FRAMESIZE ;
}
current_ogg_or_mp3_pos += n_read ;
return n_read ;
}
#endif
return n_read ;
}
int write_raw_wavefile_data(char buf[], long first, long last)
{
long n = last - first + 1 ;
int n_read ;
position_wavefile_pointer(first) ;
n_read = sf_write_raw(sndfile, buf, n*FRAMESIZE) ;
return n_read/FRAMESIZE ;
}
int read_wavefile_data(long left[], long right[], long first, long last)
{
long n = last - first + 1 ;
long s_i = 0 ;
long bufsize_long ;
long j ;
int *p_int ;
position_wavefile_pointer(first) ;
p_int = (int *)audio_buffer ;
bufsize_long = sizeof(audio_buffer) / sizeof(long) ;
while(s_i < n) {
long n_read ;
#define TRY_NEW_ABSTRACTION_NOT
#ifdef TRY_NEW_ABSTRACTION
n_read = read_raw_wavefile_data((char *)p_int, first, last) ;
#else
long n_this = MIN((n-s_i)*(stereo+1), bufsize_long) ;
if(audio_type == SNDFILE_TYPE) {
n_read = sf_read_int(sndfile, p_int, n_this) ;
}
#ifdef HAVE_OGG
if(audio_type == OGG_TYPE) {
n_read = ov_read(&oggfile, (char *)p_int, n_this*FRAMESIZE,0,2,1,¤t_ogg_bitstream) ;
n_read /= FRAMESIZE ;
}
#endif
#ifdef HAVE_MP3
if(audio_type == MP3_TYPE) {
/* size_t done ; */
/* int err = mpg123_read(fp_mp3, (unsigned char *)&buf[n_bytes_read], bufsize-n_bytes_read, &done) ; */
/* if(err != MPG123_OK) fprintf(stderr, "read had a problem, %d\n", err) ; */
/* n_read = done/FRAMESIZE ; */
}
#endif
#endif
for(j = 0 ; j < n_read ; ) {
left[s_i] = p_int[j] ;
j++ ;
if(stereo) {
right[s_i] = p_int[j] ;
j++ ;
} else {
right[s_i] = left[s_i] ;
}
s_i++ ;
}
if(n_read == 0) {
char tmp[100] ;
snprintf(tmp, sizeof(tmp), "Attempted to read past end of audio, first=%ld, last=%ld", first, last) ;
warning(tmp) ;
exit(1) ;
}
}
return s_i ;
}
int read_fft_real_wavefile_data(fftw_real left[], fftw_real right[], long first, long last)
{
long n = last - first + 1 ;
long s_i = 0 ;
long j ;
double *buffer = (double *)audio_buffer ;
int bufsize_double = sizeof(audio_buffer) / sizeof(double) ;
position_wavefile_pointer(first) ;
if(audio_type != SNDFILE_TYPE) {
long pos = first ;
short *buffer2 ;
int bufsize_short ;
buffer2 = (short *)audio_buffer2 ;
bufsize_short = sizeof(audio_buffer2) / sizeof(short) ;
while(s_i < n) {
long n_this = MIN((n-s_i), bufsize_short) ;
int n_read = read_raw_wavefile_data((char *)audio_buffer2, pos, pos+n_this-1) ;
pos += n_read ;
for(j = 0 ; j < n_read ; j++) {
left[s_i] = buffer2[j] ;
if(stereo) {
j++ ;
right[s_i] = buffer2[j] ;
} else {
right[s_i] = left[s_i] ;
}
left[s_i] /= MAXSAMPLEVALUE ;
right[s_i] /= MAXSAMPLEVALUE ;