pvxtest.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // pvxtest.c
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "sfsys.h"
  6. float ampmax(float* buffer, int samps)
  7. {
  8. double amp = 0.0;
  9. int iamp;
  10. for (iamp=0; iamp < samps ;iamp += 2) {
  11. float thisamp = buffer[iamp];
  12. amp = max(amp, thisamp);
  13. }
  14. return amp;
  15. }
  16. int main(int argc, char **argv)
  17. {
  18. SFPROPS props;
  19. int ifd, ofd, i, bufcount, sampswanted, got,put;
  20. int numsamps,numframes, pos;
  21. float *buf = NULL;
  22. float *framesbuf = NULL;
  23. float *framebufptr = NULL;
  24. int frames_per_buf = 10;
  25. int framecount = 0;
  26. int filesize = 0;
  27. int bytedatasize;
  28. if(argc< 3){
  29. fprintf(stderr, "usage: pvxtest sfile.pvx outfile.ana\n");
  30. return 1;
  31. }
  32. if(sflinit("pvxtest")){
  33. fprintf(stderr,"pvxtest: unable to initialize CDP Sound Filing System\n");
  34. return 1;
  35. }
  36. if((ifd = sndopenEx(argv[1],0,CDP_OPEN_RDONLY)) < 0){
  37. fprintf(stderr,"pvxtest: unable to open infile %s: %s\n",argv[1], sferrstr());
  38. return 1;
  39. }
  40. else {
  41. fprintf(stderr,"pvxtest: file opened OK, ifd = %d\n",ifd );
  42. }
  43. //read file size if various ways...
  44. filesize = sndsizeEx(ifd);
  45. fprintf(stderr,"got filesize = %d\n",filesize);
  46. if(!snd_headread(ifd,&props)){
  47. fprintf(stderr,"pvxtest: unable to read infile header: %s\n",sferrstr());
  48. return 1;
  49. }
  50. //read file size if various ways...
  51. filesize = sndsizeEx(ifd);
  52. fprintf(stderr,"got filesize = %d\n",filesize);
  53. /* do we have correct props? */
  54. fprintf(stderr, "props.srate: = %d\nprops.chans: %d\n",
  55. props.srate,props.chans);
  56. fprintf(stderr, "props.origsize: %d\nprops.origrate:%d\nprops.origchans:%d (pitch/fmt/trans only\n",
  57. props.origsize, props.origrate,props.origchans);
  58. fprintf(stderr,"props.arate:%f\nprops.winlen:%d\nprops.decfac :%d\n",
  59. props.arate,props.winlen,props.decfac);
  60. framesbuf = malloc(props.chans * frames_per_buf);
  61. if(framesbuf == NULL){
  62. fprintf(stderr, "alloc failure\n");
  63. return 1;
  64. }
  65. props.type = wt_analysis;
  66. props.format = WAVE;
  67. props.samptype = FLOAT32;
  68. fprintf(stderr,"setting output chans to %d\n",props.chans);
  69. //ofd = sndcreat_ex(argv[2],-1,&props,SFILE_CDP,CDP_CREATE_NORMAL);
  70. ofd = sndcreat_formatted(argv[2],-1,SAMP_FLOAT,props.chans,props.srate,CDP_CREATE_NORMAL);
  71. if(ofd < 0){
  72. fprintf(stderr,"pvxtest: unable to open outfile %s: %s\n",argv[2], sferrstr());
  73. return 1;
  74. }
  75. #ifndef SINGLEFRAME
  76. sampswanted = props.chans;
  77. buf = (float *) malloc(sizeof(float) * sampswanted);
  78. if(buf == NULL){
  79. fprintf(stderr,"failed to allocate buf of %d samps\n",sampswanted);
  80. return -1;
  81. }
  82. bufcount = 0;
  83. //for(i=0;i < 20;i++){
  84. for(i=0; /* i < numframes */; i++){
  85. got = fgetfbufEx(buf, sampswanted,ifd,0);
  86. if(got <= 0)
  87. break;
  88. if(i==0 & got == sampswanted){
  89. fprintf(stderr,"%d: read %d samps, max = %f\n",bufcount,got, ampmax(buf,got));
  90. }
  91. put = fputfbufEx(buf,sampswanted,ofd);
  92. if(put < 0){
  93. fprintf(stderr,"error writing to oputfile, block %d\n",i);
  94. }
  95. bufcount++;
  96. }
  97. fprintf(stderr,"written %d frames\n",bufcount);
  98. #else
  99. sampswanted = props.chans * frames_per_buf;
  100. for(i=0;;i++){
  101. got = fgetfbufEx(framesbuf,sampswanted,ifd,0);
  102. if(got <= 0)
  103. break;
  104. put = fputfbufEx(framesbuf,got,ifd);
  105. if(put < 0){
  106. fprintf(stderr,"error writing to oputfile, block %d\n",i);
  107. }
  108. framecount += got / props.chans;
  109. }
  110. fprintf(stderr, "processed %d frames\n",framecount);
  111. #endif
  112. //must set all CDP analysis props before closing
  113. fprintf(stderr, "adding prop orig sampsize: %d\n",props.origsize);
  114. if(sndputprop(ofd,"original sampsize",(char *) &(props.origsize),sizeof(int)) < 0){
  115. fprintf(stderr,"Failure to write original sample size %s\n", (char*)&(props.origsize));
  116. }
  117. fprintf(stderr, "adding prop orig srate: %d\n",props.origrate);
  118. if(sndputprop(ofd,"original sample rate",(char *) &(props.origrate),sizeof(int)) < 0){
  119. fprintf(stderr,"Failure to write original sample size\n");
  120. }
  121. if(sndputprop(ofd,"arate",(char*) &props.arate,sizeof(float)) < 0){
  122. fprintf(stderr,"Failure to write original sample size\n");
  123. }
  124. if(sndputprop(ofd,"analwinlen",(char*) &props.winlen,sizeof(int)) < 0){
  125. fprintf(stderr,"Failure to write original sample size\n");
  126. }
  127. if(sndputprop(ofd,"decfactor",(char*) &props.decfac,sizeof(int)) < 0){
  128. fprintf(stderr,"Failure to write original sample size\n");
  129. }
  130. #ifdef _DEBUG
  131. {
  132. // just check a couple...
  133. int got_origsize = 0,got_origrate = 0;
  134. if(sndgetprop(ofd,"original sampsize",(char*) &got_origsize,sizeof(int)) < 0)
  135. fprintf(stderr,"failed to recapture orig sampsize\n");
  136. fprintf(stderr,"got_origsize = %d\n",got_origsize);
  137. if(sndgetprop(ofd,"original sample rate",(char*) &got_origrate,sizeof(int)) < 0)
  138. fprintf(stderr,"failed to recapture orig samprate\n");
  139. fprintf(stderr,"got_origrate = %d\n",got_origrate);
  140. }
  141. #endif
  142. fprintf(stderr,"pvxtest: calling sndcloseEx(infile) (%d)\n",ifd);
  143. sndcloseEx(ifd);
  144. fprintf(stderr,"pvxtest: calling sndcloseEx(outfile) (%d)\n",ofd);
  145. sndcloseEx(ofd);
  146. return 0;
  147. }