dump.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <structures.h>
  24. #include <tkglobals.h>
  25. #include <pnames.h>
  26. #include <globcon.h>
  27. #include <cdpmain.h>
  28. #include <house.h>
  29. #include <modeno.h>
  30. #include <sfsys.h>
  31. #include <memory.h>
  32. #include "sfdump.h"
  33. /*************************** HOUSE_BAKUP ******************************/
  34. int house_bakup(dataptr dz)
  35. {
  36. int n, exit_status;
  37. int samps_to_write, secs_to_write, insert_samps, insert_secs;
  38. int spare_samps, tail, spare_samps_in_secs, samps_written = 0;
  39. float *buf = dz->bigbuf;
  40. insert_samps = dz->infile->srate * dz->infile->channels; /* 1 second in samples */
  41. insert_samps = (int)round(BAKUP_GAP * insert_samps);
  42. if((insert_secs = insert_samps/F_SECSIZE) * F_SECSIZE != insert_samps)
  43. insert_secs++;
  44. insert_samps = insert_secs * F_SECSIZE;
  45. dz->tempsize = 0;
  46. for(n=0;n<dz->infilecnt;n++)
  47. dz->tempsize += dz->insams[n];
  48. for(n=0;n<dz->infilecnt;n++) {
  49. sndseekEx(dz->ifd[n],0,0);
  50. dz->samps_left = dz->insams[n];
  51. spare_samps = 0;
  52. while(dz->samps_left > 0) {
  53. memset((char *)dz->bigbuf,0,dz->buflen * sizeof(float));
  54. if((dz->ssampsread = fgetfbufEx(buf,dz->buflen,dz->ifd[n],0)) < 0) {
  55. sprintf(errstr,"Can't read samples from input soundfile %d\n",n+1);
  56. return(SYSTEM_ERROR);
  57. }
  58. dz->samps_left -= dz->ssampsread;
  59. samps_to_write = dz->ssampsread; /* Default (full input buff) write all of it */
  60. spare_samps = dz->buflen - dz->ssampsread;
  61. if(spare_samps) { /* with partially full input buff */
  62. tail = spare_samps % F_SECSIZE;
  63. spare_samps_in_secs = spare_samps - tail;
  64. if(spare_samps_in_secs >= insert_samps) { /* If empty part of buf exceeds silence-insert-dur */
  65. samps_to_write = dz->ssampsread + tail + insert_samps; /* write ALL silence from buftail */
  66. if(((samps_to_write/F_SECSIZE) * F_SECSIZE) != samps_to_write) {
  67. sprintf(errstr,"Error in program file-write logic.\n");
  68. return(PROGRAM_ERROR);
  69. }
  70. } else {
  71. samps_to_write = dz->buflen; /* Else, write some of silence from tail of buffer */
  72. if((exit_status = write_samps(buf,samps_to_write,dz)) < 0)
  73. return(exit_status);
  74. dz->total_samps_written += samps_written;
  75. display_virtual_time(dz->total_samps_written,dz);
  76. memset((char *)dz->bigbuf,0,dz->buflen * sizeof(float));
  77. samps_to_write = insert_samps - spare_samps;
  78. if((secs_to_write = samps_to_write/F_SECSIZE) * F_SECSIZE != samps_to_write)
  79. secs_to_write++; /* rounding up to whole number of sectors */
  80. samps_to_write = secs_to_write * F_SECSIZE;
  81. }
  82. }
  83. if(samps_to_write > 0) {
  84. if((exit_status = write_samps(buf, samps_to_write,dz)) < 0)
  85. return(exit_status);
  86. }
  87. dz->total_samps_written += samps_written;
  88. display_virtual_time(dz->total_samps_written,dz);
  89. }
  90. if(spare_samps == 0) { /* in event of last file write EXACTLY fitting into read buffer */
  91. memset((char *)dz->bigbuf,0,dz->buflen * sizeof(float));
  92. if(insert_samps > 0) {
  93. if((exit_status = write_samps(buf, insert_samps,dz)) < 0)
  94. return(exit_status);
  95. }
  96. dz->total_samps_written += samps_written;
  97. display_virtual_time(dz->total_samps_written,dz);
  98. }
  99. }
  100. return FINISHED;
  101. }