/CS/migrated/branches/R0_17/plugins/sound/loader/aufile.cpp

# · C++ · 157 lines · 115 code · 21 blank · 21 comment · 20 complexity · 414493d4982b027c5807d46bca72869d MD5 · raw file

  1. /*
  2. Copyright (C) 1998 by Jorrit Tyberghein
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this library; if not, write to the Free
  13. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include <math.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "cssysdef.h"
  20. #include "csutil/csvector.h"
  21. #include "sndload.h"
  22. // Sun AU file loader
  23. // support 8 and 16 bits PCM
  24. // and 8 bit ULAW (no compressed)
  25. #define BIT16 0x03
  26. #define BIT8 0x02
  27. #define BIT8ULAW 0x01
  28. #define setStream(x) {if(x>size) {goto exit_read;} else {index=x;}}
  29. #define canAddStream(x) {if((index+x)>size) goto exit_read;}
  30. #define addStream(x) {if((index+x)>size) {goto exit_read;} else {index+=x;}}
  31. #define Stream buf[index]
  32. const char *csSoundLoader_AU::GetDesc() const {
  33. return "Sun sound format";
  34. }
  35. iSoundData* csSoundLoader_AU::Load(UByte* buf, ULong size,
  36. const csSoundFormat *fmt) const {
  37. unsigned long index=0;
  38. csSoundDataWave *sb= NULL;
  39. char *data=NULL;
  40. unsigned char dummy0, dummy1, dummy2, dummy3;
  41. unsigned long offset, nbytes, flag, freq, nchannels;
  42. if(memcmp(&Stream, ".snd", 4))
  43. goto exit_read;
  44. addStream(4);
  45. dummy0 = Stream; addStream(1);
  46. dummy1 = Stream; addStream(1);
  47. dummy2 = Stream; addStream(1);
  48. dummy3 = Stream; addStream(1);
  49. offset = csSndFunc::makeDWord(dummy0, dummy1, dummy2, dummy3);
  50. dummy0 = Stream; addStream(1);
  51. dummy1 = Stream; addStream(1);
  52. dummy2 = Stream; addStream(1);
  53. dummy3 = Stream; addStream(1);
  54. nbytes = csSndFunc::makeDWord(dummy0, dummy1, dummy2, dummy3);
  55. dummy0 = Stream; addStream(1);
  56. dummy1 = Stream; addStream(1);
  57. dummy2 = Stream; addStream(1);
  58. dummy3 = Stream; addStream(1);
  59. flag = csSndFunc::makeDWord(dummy0, dummy1, dummy2, dummy3);
  60. if(flag!=BIT16 && flag!=BIT8 && flag!=BIT8ULAW)
  61. goto exit_read;
  62. dummy0 = Stream; addStream(1);
  63. dummy1 = Stream; addStream(1);
  64. dummy2 = Stream; addStream(1);
  65. dummy3 = Stream; addStream(1);
  66. freq = csSndFunc::makeDWord(dummy0, dummy1, dummy2, dummy3);
  67. dummy0 = Stream; addStream(1);
  68. dummy1 = Stream; addStream(1);
  69. dummy2 = Stream; addStream(1);
  70. dummy3 = Stream; addStream(1);
  71. nchannels = csSndFunc::makeDWord(dummy0, dummy1, dummy2, dummy3);
  72. if(nchannels>2 || nchannels<1)
  73. goto exit_read;
  74. canAddStream(nbytes);
  75. if(flag==BIT8)
  76. {
  77. data=new char[nbytes];
  78. if (data==NULL)
  79. goto exit_read;
  80. unsigned long i=0;
  81. char *ptr=(char *)data;
  82. while(i<nbytes)
  83. {
  84. dummy0 = Stream; addStream(1);
  85. // datas are stored in unsigned 8 bit but mixer engine only support signed 8 bit
  86. *ptr++=dummy0-128;
  87. i++;
  88. }
  89. }
  90. else if(flag==BIT16)
  91. {
  92. data=new char[nbytes];
  93. if(data==NULL)
  94. goto exit_read;
  95. int i=0;
  96. int nbs = nbytes/2;
  97. unsigned short *ptr=(unsigned short *)data;
  98. while(i<nbs)
  99. {
  100. dummy0 = Stream; addStream(1);
  101. dummy1 = Stream; addStream(1);
  102. *ptr++=csSndFunc::makeWord(dummy0, dummy1);
  103. i++;
  104. }
  105. }
  106. else if(flag==BIT8ULAW)
  107. {
  108. data=new char[nbytes*2];
  109. if(data==NULL)
  110. goto exit_read;
  111. int i=0;
  112. int nbs = nbytes;
  113. unsigned short *ptr=(unsigned short *)data;
  114. while(i<nbs)
  115. {
  116. dummy0 = Stream; addStream(1);
  117. *ptr++=csSndFunc::ulaw2linear(dummy0);
  118. i++;
  119. }
  120. }
  121. sb=new csSoundDataWave(NULL);
  122. sb->Initialize(freq,
  123. (flag==BIT16 || flag==BIT8ULAW)?16:8,
  124. nchannels,
  125. (flag==BIT16)?(nbytes/2)-1:nbytes-1,
  126. (unsigned char*)data);
  127. sb->Prepare(fmt);
  128. goto exit_ok;
  129. exit_read:
  130. delete [] data;
  131. exit_ok:
  132. return sb;
  133. }