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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
|
/*****************************************************************************
* 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 ;
if(first == 0) {
/* fprintf(stderr, "%4d %d %d\n", (int)s_i, (int)left[s_i], (int)right[s_i]) ; */
}
s_i++ ;
}
if(n_read == 0) {
char tmp[100] ;
snprintf(tmp, sizeof(tmp), "read_fft_real Attempted to read past end of audio, first=%ld, last=%ld", first, last) ;
warning(tmp) ;
//exit(1) ;
}
}
} else {
while(s_i < n) {
long n_this = MIN((n-s_i)*(stereo+1), bufsize_double) ;
long n_read = sf_read_double(sndfile, buffer, n_this) ;
for(j = 0 ; j < n_read ; j++) {
left[s_i] = buffer[j] ;
if(stereo) {
j++ ;
right[s_i] = buffer[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_float_wavefile_data(float left[], float right[], long first, long last)
{
long n = last - first + 1 ;
long s_i = 0 ;
long j ;
float *buffer = (float *)audio_buffer ;
int bufsize_float = sizeof(audio_buffer) / sizeof(float) ;
position_wavefile_pointer(first) ;
while(s_i < n) {
long n_this = MIN((n-s_i)*(stereo+1), bufsize_float) ;
long n_read = sf_read_float(sndfile, buffer, n_this) ;
for(j = 0 ; j < n_read ; j++) {
left[s_i] = buffer[j] ;
if(stereo) {
j++ ;
right[s_i] = buffer[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 sf_write_values(void *ptr, int n_samples)
{
int n = 0 ;
if(n_samples > 0) {
n = sf_write_int(sndfile, ptr, n_samples) ;
}
return n * BYTESPERSAMPLE ;
}
long n_in_buf = 0 ;
int WRITE_VALUE_TO_AUDIO_BUF(char *ivalue)
{
int i ;
int n_written = 0 ;
if(BYTESPERSAMPLE+n_in_buf > MAXBUFSIZE) {
n_written = sf_write_values(audio_buffer, n_in_buf/BYTESPERSAMPLE) ;
n_in_buf = 0 ;
}
for(i = 0 ; i < BYTESPERSAMPLE ; i++, n_in_buf++)
audio_buffer[n_in_buf] = ivalue[i] ;
return n_written ;
}
#define FLUSH_AUDIO_BUF(n_written) {\
n_written += sf_write_values(audio_buffer, n_in_buf/BYTESPERSAMPLE) ;\
n_in_buf = 0 ;\
}
int write_fft_real_wavefile_data(fftw_real left[], fftw_real right[], long first, long last)
{
long n = last - first + 1 ;
long j ;
long n_written = 0 ;
/* make SURE MAXBUF is a multiple of 2 */
#define MAXBUF 500
double buf[MAXBUF] ;
n_in_buf = 0 ;
position_wavefile_pointer(first) ;
for(j = 0 ; j < n ; j++) {
buf[n_in_buf] = left[j] ;
n_in_buf++ ;
if(stereo) {
buf[n_in_buf] = right[j] ;
n_in_buf++ ;
}
if(n_in_buf == MAXBUF) {
n_written += sf_write_double(sndfile, buf, n_in_buf) ;
n_in_buf = 0 ;
}
}
if(n_in_buf > 0) {
n_written += sf_write_double(sndfile, buf, n_in_buf) ;
n_in_buf = 0 ;
}
return n_written/2 ;
#undef MAXBUF
}
int write_float_wavefile_data(float left[], float right[], long first, long last)
{
long n = last - first + 1 ;
long j ;
long n_written = 0 ;
/* make SURE MAXBUF is a multiple of 2 */
#define MAXBUF 500
float buf[MAXBUF] ;
n_in_buf = 0 ;
position_wavefile_pointer(first) ;
for(j = 0 ; j < n ; j++) {
buf[n_in_buf] = left[j] ;
n_in_buf++ ;
if(stereo) {
buf[n_in_buf] = right[j] ;
n_in_buf++ ;
}
if(n_in_buf == MAXBUF) {
n_written += sf_write_float(sndfile, buf, n_in_buf) ;
n_in_buf = 0 ;
}
}
if(n_in_buf > 0) {
n_written += sf_write_float(sndfile, buf, n_in_buf) ;
n_in_buf = 0 ;
}
return n_written/2 ;
#undef MAXBUF
}
int write_wavefile_data(long left[], long right[], long first, long last)
{
long n = last - first + 1 ;
long j ;
long n_written = 0 ;
/* make SURE MAXBUF is a multiple of 2 */
#define MAXBUF 500
int buf[MAXBUF] ;
n_in_buf = 0 ;
position_wavefile_pointer(first) ;
for(j = 0 ; j < n ; j++) {
buf[n_in_buf] = left[j] ;
n_in_buf++ ;
if(stereo) {
buf[n_in_buf] = right[j] ;
n_in_buf++ ;
}
if(n_in_buf == MAXBUF) {
n_written += sf_write_int(sndfile, buf, n_in_buf) ;
n_in_buf = 0 ;
}
}
if(n_in_buf > 0) {
n_written += sf_write_int(sndfile, buf, n_in_buf) ;
n_in_buf = 0 ;
}
return n_written/2 ;
#undef MAXBUF
}
void flush_wavefile_data(void)
{
fsync(wavefile_fd) ;
}
/* process_audio for mac_os_x is found in the audio_osx.c */
#if !defined MAC_OS_X || defined HAVE_PULSE_AUDIO
int process_audio(gfloat *pL, gfloat *pR)
{
int len = 0 ;
int i, frame ;
short *p_short ;
int *p_int ;
unsigned char *p_char ;
short maxl = 0, maxr = 0 ;
extern int audio_playback ;
long n_samples_to_read, n_read ;
double maxpossible ;
double feather_out_N ;
int feather_out = 0 ;
*pL = 0.0 ;
*pR = 0.0 ;
if(audio_state == AUDIO_IS_IDLE) {
d_print("process_audio says NOTHING is going on.\n") ;
return 1 ;
}
if(audio_state == AUDIO_IS_RECORDING) {
if((len = audio_device_read(audio_buffer, BUFSIZE)) == -1) {
warning("Error on audio read...") ;
}
} else if(audio_state == AUDIO_IS_PLAYBACK) {
len = audio_device_nonblocking_write_buffer_size(
MAXBUFSIZE, playback_samples_remaining*PLAYBACK_FRAMESIZE);
if (len <= 0) {
return 0 ;
}
}
n_samples_to_read = len/PLAYBACK_FRAMESIZE ;
if(n_samples_to_read*PLAYBACK_FRAMESIZE != len)
g_print("ACK!!\n") ;
p_char = (unsigned char *)audio_buffer ;
p_short = (short *)audio_buffer ;
p_int = (int *)audio_buffer ;
/* for now force playback to 16 bit... */
#define BYTESPERSAMPLE 2
if(audio_type == SNDFILE_TYPE) {
if(BYTESPERSAMPLE < 3) {
maxpossible = 1 << 15 ;
n_read = sf_readf_short(sndfile, p_short, n_samples_to_read) ;
} else {
maxpossible = 1 << 23 ;
n_read = sf_readf_int(sndfile, p_int, n_samples_to_read) ;
}
} else {
#if defined(HAVE_MP3) || defined(HAVE_OGG)
maxpossible = 1 << 15 ;
n_read = read_raw_wavefile_data((char *)p_char, current_ogg_or_mp3_pos, current_ogg_or_mp3_pos+n_samples_to_read-1) ;
#endif
}
#define FEATHER_WIDTH 30000
if(playback_samples_remaining - n_read < 0) {
feather_out = 1 ;
feather_out_N = MIN(n_read, FEATHER_WIDTH) ;
fprintf(stderr, "Feather out n_read=%ld, playback_samples_remaining=%ld, N=%lf\n", n_read, playback_samples_remaining, feather_out_N) ;
}
for(frame = 0 ; frame < n_read ; frame++) {
int vl, vr ;
// I don't understand how all this code works, but if I remove the multiplication by two then the level meter actually works for mono files in ALSA, and for some reason there seem to be no side effects.
// However, note that the level meters work quite differently in different configurations, and arguably don't show anything helpful, anyway.
// Perhaps we should multiply by (stereo + 1) though?
// i = frame*2 ;
i = frame;
if(BYTESPERSAMPLE < 3) {
if(feather_out == 1 && n_read-(frame+1) < FEATHER_WIDTH) {
int j = ((n_read-(frame))-1) ;
double p = (double)(j)/feather_out_N ;
if(i > n_read - 100) {
//printf("j:%d %lf %hd %hd ", j, p, p_short[i], p_short[i+1]) ;
}
p_short[i] *= p ;
p_short[i+1] *= p ;
//if(i > n_read - 100) {
// printf("%hd %hd\n", p_short[i], p_short[i+1]) ;
//}
if(frame == n_read-1) fprintf(stderr, "Feather out final %lf, n_read=%ld\n", p, n_read) ;
}
vl = p_short[i] ;
vr = p_short[i+1] ;
} else {
if(feather_out == 1 && n_read-(i+1) < 10000) {
double p = 1.0 - (double)(n_read-(i+1))/9999.0 ;
printf(".") ;
p_int[i] *= p ;
p_int[i+1] *= p ;
}
vl = p_int[i] ;
vr = p_int[i+1] ;
}
if(vl > maxl) maxl = vl ;
if(-vl > maxl) maxl = -vl ;
if(stereo) {
if(vr > maxr) maxr = vr ;
if(-vr > maxr) maxr = -vr ;
} else {
maxr = maxl ;
}
}
#undef BYTESPERSAMPLE
if(feather_out == 1) printf("\n") ;
*pL = (gfloat) maxl / maxpossible ;
*pR = (gfloat) maxr / maxpossible ;
if(audio_state == AUDIO_IS_RECORDING) {
len = write(wavefile_fd, audio_buffer, len) ;
audio_bytes_written += len ;
} else if(audio_state == AUDIO_IS_PLAYBACK) {
len = audio_device_write(p_char, len) ;
playback_position += n_read ;
playback_samples_remaining -= n_read ;
if(playback_samples_remaining < 1) {
extern int audio_is_looping ;
if(audio_is_looping == FALSE) {
unsigned char zeros[1024] ;
long zeros_needed ;
memset(zeros,0,sizeof(zeros)) ;
audio_state = AUDIO_IS_PLAYBACK ;
audio_playback = FALSE ;
zeros_needed = playback_bytes_per_block - (playback_total_bytes % playback_bytes_per_block) ;
if(zeros_needed < PLAYBACK_FRAMESIZE) zeros_needed = PLAYBACK_FRAMESIZE ;
do {
len = audio_device_write(zeros, MIN(zeros_needed, sizeof(zeros))) ;
zeros_needed -= len ;
} while (len >= 0 && zeros_needed > 0) ;
g_print("Stop playback with playback_samples_remaining:%ld\n", playback_samples_remaining) ;
return 1 ;
} else {
playback_position = playback_start_position ;
playback_samples_remaining = (playback_end_position-playback_start_position) ;
sf_seek(sndfile, playback_position, SEEK_SET) ;
g_print("Loop with playback_samples_remaining:%ld\n", playback_samples_remaining) ;
looped_count++ ;
}
}
}
return 0 ;
}
#endif
void stop_playback(int force)
{
if(!force) {
/* Robert altered */
int new_playback = audio_device_processed_bytes();
int old_playback;
while(new_playback < playback_total_bytes) {
/* Robert altered */
usleep(100) ;
old_playback = new_playback;
new_playback=audio_device_processed_bytes();
/* check if more samples have been processed, if not,quit */
if (old_playback==new_playback){
fprintf(stderr,"Playback appears frozen\n Breaking\n");
break;
}
}
usleep(100) ;
}
/* fprintf(stderr, "Usleeping 300000\n") ; */
/* usleep(300000) ; */
/* fprintf(stderr, "Done usleeping 300000\n") ; */
audio_state = AUDIO_IS_IDLE ;
audio_device_close(1-force) ;
}
|