abfdcode2.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 1983-2013 Richard Dobson and Composers Desktop Project Ltd
  3. * http://people.bath.ac.uk/masrwd
  4. * http://www.composersdesktop.com
  5. * This file is part of the CDP System.
  6. * The CDP System is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  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.
  14. * See the 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 02111-1307 USA
  18. *
  19. */
  20. //abfdcode2.cpp : decode to std wavex speaker positions
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <math.h>
  24. extern "C"{
  25. #include <sfsys.h>
  26. }
  27. typedef struct abf_samp {
  28. float W;
  29. float X;
  30. float Y;
  31. float Z;
  32. } ABFSAMPLE;
  33. int main(int argc,char *argv[])
  34. {
  35. int ifd, ofd;
  36. long outchans = 4;
  37. long outsize;
  38. char *sfname;
  39. bool write_wavex = false;
  40. ABFSAMPLE abfsample;
  41. float frame[4];
  42. SFPROPS props;
  43. CHPEAK peaks[4];
  44. long inchans;
  45. if(argc < 3){
  46. printf("\nCDP MCTOOLS: ABFDCODE2 v 1.0: RWD,CDP 2009\n"
  47. "Horizontal B Format Decoder\n"
  48. "usage: abfdcode2 infile outfile layout\n"
  49. " (wavex layouts require .wav extension)\n"
  50. );
  51. return 1;
  52. }
  53. while(argv[1][0] =='-'){
  54. switch(argv[1][1]){
  55. case('x'):
  56. write_wavex = true;
  57. break;
  58. default:
  59. fprintf(stderr,"abfdecode: error: illegal flag option %s\n",argv[1]);
  60. return 1;
  61. }
  62. argc--; argv++;
  63. }
  64. if(argc < 3){
  65. fprintf(stderr,"CDP MCTOOLS: ABFDCODE.EXE: CDP 1999,2005\nHorizontal B Format Decoder\nusage: abfdcode [-x] infile outfile\n");
  66. return 1;
  67. }
  68. if(sflinit("abfdcode")){
  69. fprintf(stderr,"unable to initialize sfsys\n");
  70. return 1;
  71. }
  72. sfname = argv[2];
  73. ifd = sndopenEx(argv[1],0,CDP_OPEN_RDONLY);
  74. if(ifd < 0){
  75. fprintf(stderr,"unable toopen infile %s\n",argv[1]);
  76. return 1;
  77. }
  78. if(!snd_headread(ifd,&props)){
  79. fprintf(stderr,"unable to read infile header data\n");
  80. sndcloseEx(ifd);
  81. return 1;
  82. }
  83. if(!props.type==wt_wave){
  84. fprintf(stderr,"infile is not a soundfile\n");
  85. sndcloseEx(ifd);
  86. return 1;
  87. }
  88. inchans = props.chans;
  89. if(!(inchans == 3 || inchans == 4)){
  90. fprintf(stderr,"Sorry: infile is not first-order\n");
  91. sndcloseEx(ifd);
  92. return 1;
  93. }
  94. outsize = sndsizeEx(ifd) / inchans;
  95. if(outsize <= 0){
  96. fprintf(stderr,"infile is empty!\n");
  97. sndcloseEx(ifd);
  98. return 1;
  99. }
  100. //srate = props.srate;
  101. //stype = props.samptype == FLOAT32 ? SAMP_FLOAT : SAMP_SHORT;
  102. props.chformat = STDWAVE;
  103. props.chans = 4;
  104. if(write_wavex){
  105. props.chformat = MC_QUAD;
  106. props.format = WAVE_EX;
  107. }
  108. //ofd = sndcreat_formatted(sfname,outsize * outchans,stype,outchans,srate,CDP_CREATE_NORMAL);
  109. ofd = sndcreat_ex(sfname,outsize * outchans,&props,SFILE_CDP,CDP_CREATE_NORMAL);
  110. if(ofd < 0){
  111. fprintf(stderr,"can't create outfile %s : %s\n",sfname,sferrstr());
  112. //delete p_iosc;
  113. //delete p_sintable;
  114. sndcloseEx(ifd);
  115. return 1;
  116. }
  117. peaks[0].value = peaks[1].value = peaks[2].value = peaks[3].value = 0.0f;
  118. peaks[0].position = peaks[1].position = peaks[2].position = peaks[3].position = 0;
  119. int got,quartersec = props.srate / 4;
  120. unsigned int framepos = 0;
  121. printf("\ndoing b-format decoding:\n");
  122. while((got = fgetfbufEx((float *) &abfsample,inchans,ifd,0))==inchans){
  123. int i;
  124. //this_samp = 0.5 * p_iosc->tick(1000);
  125. float aw = abfsample.W;
  126. float ax = abfsample.X * 0.707f;
  127. float ay = abfsample.Y * 0.707f;
  128. frame[0] = 0.3333f * (aw + ax + ay);
  129. frame[1] = 0.3333f * (aw + ax - ay);
  130. frame[2] = 0.3333f * (aw - ax + ay);
  131. frame[3] = 0.3333f * (aw - ax - ay);
  132. if(0 > fputfbufEx(frame,outchans,ofd)){
  133. fprintf(stderr,"error writing sample block %ld\n",got * outchans);
  134. sndcloseEx(ifd);
  135. sndcloseEx(ofd);
  136. return 1;
  137. }
  138. for(i=0;i < 4;i++){
  139. float val;
  140. val= (float)fabs(frame[i]);
  141. if(val > peaks[i].value) {
  142. peaks[i].value = val;
  143. peaks[i].position = framepos;
  144. }
  145. }
  146. if(framepos % quartersec==0)
  147. printf("%.2lf secs\r",(double)framepos / (double) props.srate);
  148. framepos++;
  149. }
  150. if(got != 0){
  151. fprintf(stderr,"warning: not all data was read\n");
  152. }
  153. printf("\n%.4lf secs\nWritten %d quad frames to %s\n",(double)framepos / (double) props.srate,framepos,sfname);
  154. printf("PEAK values:\n");
  155. printf("CH 1: %.4f at frame %ld:\t%.4f secs\n",peaks[0].value,peaks[0].position,(double)peaks[0].position / (double) props.srate);
  156. printf("CH 2: %.4f at frame %ld:\t%.4f secs\n",peaks[1].value,peaks[1].position,(double)peaks[1].position / (double) props.srate);
  157. printf("CH 3: %.4f at frame %ld:\t%.4f secs\n",peaks[2].value,peaks[2].position,(double)peaks[2].position / (double) props.srate);
  158. printf("CH 4: %.4f at frame %ld:\t%.4f secs\n",peaks[3].value,peaks[3].position,(double)peaks[3].position / (double) props.srate);
  159. sndputpeaks(ofd,4,peaks);
  160. sndcloseEx(ifd);
  161. sndcloseEx(ofd);
  162. sffinish();
  163. return 0;
  164. }