envxtract.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /* floatsam version */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <structures.h>
  25. #include <tkglobals.h>
  26. #include <globcon.h>
  27. #include <processno.h>
  28. #include <modeno.h>
  29. #include <arrays.h>
  30. #include <envel.h>
  31. #include <cdpmain.h>
  32. #include <sfsys.h>
  33. static void getenv_of_buffer
  34. (int samps_to_process,int envwindow_sampsize,double convertor,float **envptr,float *buffer);
  35. static double getmaxsamp(int startsamp, int cnt,float *buffer);
  36. /******************************** EXTRACT_ENV_FROM_SNDFILE [GETENV] *****************************
  37. *
  38. * NB THIS ALWAYS GETS ENVELOPE FROM sampbuf[0] !!!
  39. */
  40. int extract_env_from_sndfile(int *bufcnt,int *envcnt,float **env,float **envend,int fileno,dataptr dz)
  41. {
  42. int n;
  43. double convertor = 1.0/F_ABSMAXSAMP;
  44. float *envptr;
  45. int envwindow_sampsize = dz->iparam[ENV_SAMP_WSIZE];
  46. float *buffer = dz->sampbuf[0];
  47. *bufcnt = buffers_in_sndfile(dz->buflen,fileno,dz);
  48. *envcnt = windows_in_sndfile(fileno,dz);
  49. if((*env=(float *)malloc((*envcnt+20) * sizeof(float)))==NULL) {
  50. sprintf(errstr,"INSUFFICIENT MEMORY for envelope array.\n");
  51. return(MEMORY_ERROR);
  52. }
  53. envptr = *env;
  54. for(n = 0; n < *bufcnt; n++) {
  55. if((dz->ssampsread = fgetfbufEx(dz->sampbuf[0], dz->buflen,dz->ifd[fileno],0)) < 0) {
  56. sprintf(errstr,"Can't read samples from soundfile: extract_env_from_sndfile()\n");
  57. return(SYSTEM_ERROR);
  58. }
  59. if(sloom)
  60. display_virtual_time(dz->total_samps_read,dz);
  61. getenv_of_buffer(dz->ssampsread,envwindow_sampsize,convertor,&envptr,buffer);
  62. }
  63. *envend = envptr;
  64. return(FINISHED);
  65. }
  66. /************************* GETENV_OF_BUFFER [READENV] *******************************/
  67. void getenv_of_buffer(int samps_to_process,int envwindow_sampsize,double convertor,float **envptr,float *buffer)
  68. {
  69. int start_samp = 0;
  70. float *env = *envptr;
  71. while(samps_to_process >= envwindow_sampsize) {
  72. *env++ = (float) (getmaxsamp(start_samp,envwindow_sampsize,buffer) * convertor);
  73. start_samp += envwindow_sampsize;
  74. samps_to_process -= envwindow_sampsize;
  75. }
  76. if(samps_to_process) /* Handle any final short buffer */
  77. *env++ = (float)(getmaxsamp(start_samp,samps_to_process,buffer) * convertor);
  78. *envptr = env;
  79. }
  80. /****************************** BUFFERS_IN_SNDFILE [BUFFER_CNT] ******************************/
  81. int buffers_in_sndfile(int buffer_size,int fileno,dataptr dz)
  82. {
  83. int bufcnt;
  84. if(((bufcnt = dz->insams[fileno]/buffer_size)*buffer_size)!=dz->insams[0])
  85. bufcnt++;
  86. return(bufcnt);
  87. }
  88. /****************************** WINDOWS_IN_SNDFILE [GET_ENVSIZE] ******************************
  89. *
  90. * Find samp-length of soundfiles, number of whole buffers this corresponds
  91. * to (bufcnt) and number of whole sectors reamining (nsec).
  92. */
  93. int windows_in_sndfile(int fileno,dataptr dz)
  94. {
  95. int envsize, winsize = dz->iparam[ENV_SAMP_WSIZE];
  96. if(((envsize = dz->insams[fileno]/winsize) * winsize)!=dz->insams[fileno])
  97. envsize++;
  98. return(envsize);
  99. }
  100. /*************************** GETMAXSAMP ******************************
  101. *
  102. * REVERTED TO FINDING largest sample in window, rather than POWER.
  103. */
  104. /*int*/double getmaxsamp(int startsamp, int sampcnt,float *buffer)
  105. {
  106. int i, endsamp = startsamp + sampcnt;
  107. double thisval, thismaxsamp = 0.0;
  108. for(i = startsamp; i<endsamp; i++) {
  109. if((thisval = fabs(buffer[i]))>thismaxsamp)
  110. thismaxsamp = thisval;
  111. }
  112. return thismaxsamp;
  113. }