Voc_c.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "include.hpp"
  2. #ifdef WITH_VOC_EDITOR
  3. #define NO_DISPLAY_LINES 10
  4. #define DISPLAY_LINE_LEN 80
  5. char **lines_text;
  6. int voc_file = 0;
  7. char * voc_data = NULL;
  8. int voc_data_size = 0;
  9. void display_lines(void);
  10. void expand_voc(void);
  11. void load_voc_file();
  12. void play_voc_chunk(int,int);
  13. void play_voc_data(char *,int,int);
  14. void redisplay_line(int,char *);
  15. void update_line(int);
  16. void bincpy(void *,void *,int);
  17. void voc_editor()
  18. {
  19. int count,leave=0;
  20. debug_printf("Voc editor start...");
  21. // Play around with voc files
  22. lines_text = malloc(NO_DISPLAY_LINES*sizeof(char *));
  23. for (count=0; count<NO_DISPLAY_LINES; count++)
  24. { lines_text[count] = malloc(DISPLAY_LINE_LEN);
  25. lines_text[count][0] = 0;
  26. }
  27. sprintf(lines_text[0],"Voc Editor.");
  28. update_line(0);
  29. while (!leave)
  30. { display_lines();
  31. switch(mgetch())
  32. { case 'l': // Load in a voc file
  33. load_voc_file();
  34. break;
  35. case 'p':
  36. if (voc_data_size)
  37. play_voc_chunk(0,100000);
  38. else
  39. { sprintf(lines_text[1],"No voc file to play");
  40. update_line(1);
  41. }
  42. break;
  43. case 'x': // Pad out data
  44. if (voc_data_size)
  45. expand_voc();
  46. else
  47. { sprintf(lines_text[1],"No voc file to expand");
  48. update_line(1);
  49. }
  50. break;
  51. case 27:
  52. leave = 1;
  53. break;
  54. }
  55. }
  56. for (count=0; count<NO_DISPLAY_LINES; count++)
  57. free(lines_text[count]);
  58. free(lines_text);
  59. if (voc_data_size)
  60. free(voc_data);
  61. voc_data = (char *)(voc_file = voc_data_size = 0);
  62. debug_printf("Voc editor start...");
  63. }
  64. void update_line(int line_num)
  65. {
  66. redisplay_line(line_num,lines_text[line_num]);
  67. }
  68. void load_voc_file()
  69. { char name[9];
  70. int got_name = 0,cursor = 0,count;
  71. sprintf(lines_text[1],"Enter voc name...");
  72. update_line(1);
  73. display_lines();
  74. while (!got_name)
  75. { char ch = mgetch();
  76. if ( (ch > ' ') && (ch < 126) )
  77. { name[cursor++] = ch;
  78. name[cursor] = 0;
  79. if (cursor >= 8)
  80. got_name = 1;
  81. else
  82. { sprintf(lines_text[1],"Load %s.voc",name);
  83. update_line(1);
  84. display_lines();
  85. }
  86. }
  87. else switch(ch)
  88. { case 13:
  89. got_name = 1;
  90. break;
  91. case 27:
  92. got_name = (-1);
  93. break;
  94. }
  95. }
  96. if (got_name == 1)
  97. { char filename[13];
  98. sprintf(filename,"%s.voc",name);
  99. voc_file = open(filename,O_RDONLY,0);
  100. if (voc_file < 0)
  101. { sprintf(lines_text[1],"%s.voc not found",name);
  102. update_line(1);
  103. voc_file=0;
  104. }
  105. else
  106. { struct stat status;
  107. char *ldata,*lpointer;
  108. if (voc_data_size)
  109. { free(voc_data);
  110. voc_data_size = 0;
  111. }
  112. fstat(voc_file,&status);
  113. ldata = malloc(status.st_size);
  114. read(voc_file,ldata,status.st_size);
  115. close(voc_file);
  116. // Combine all the data together
  117. if (strncmp(ldata,"Creative Voice File",19)!=0)
  118. { debug_printf("Not a Voc file");
  119. pc_restore();
  120. exit(1);
  121. }
  122. debug_printf("Data offset %x",*((short *)(ldata+20)));
  123. debug_printf("Version %x",*((short *)(ldata+22)));
  124. debug_printf("code %x",*((short *)(ldata+24)));
  125. lpointer = ldata + *((short *)(ldata+20));
  126. while (*lpointer) // while not block type 0
  127. { debug_printf("Block type %d",*lpointer);
  128. switch(*lpointer)
  129. { case 1: // Sound data block
  130. { int bsize = *((int *)(lpointer+1));
  131. bsize &= 0xffffff;
  132. debug_printf("Old block size %d",bsize);
  133. if (voc_data_size)
  134. voc_data = realloc(voc_data,voc_data_size + bsize);
  135. else voc_data = malloc(voc_data_size + bsize);
  136. bincpy(voc_data+voc_data_size,lpointer+6,bsize-2);
  137. voc_data_size += (bsize-2);
  138. lpointer += (bsize+4);
  139. }
  140. break;
  141. case 9: // New sound data block
  142. { int bsize = *((int *)(lpointer+1));
  143. bsize &= 0xffffff;
  144. debug_printf("New block size %d",bsize);
  145. if (voc_data_size)
  146. voc_data = realloc(voc_data,voc_data_size + bsize);
  147. else voc_data = malloc(voc_data_size + bsize);
  148. bincpy(voc_data+voc_data_size,lpointer+16,bsize-12);
  149. voc_data_size += (bsize-12);
  150. lpointer += (bsize+4);
  151. }
  152. break;
  153. default:
  154. debug_printf("Invalid type %d",*lpointer);
  155. *lpointer = 0;
  156. break;
  157. }
  158. }
  159. sprintf(lines_text[1],"Voc file : %s Size %d",name,voc_data_size);
  160. update_line(1);
  161. }
  162. }
  163. }
  164. void play_voc_chunk(int start,int len)
  165. {
  166. if (start >= voc_data_size)
  167. return;
  168. if ( (start+len) > voc_data_size)
  169. len = voc_data_size - start;
  170. if (len >= 65000)
  171. len = 65000;
  172. play_voc_data(voc_data + start, len, 0);
  173. }
  174. void expand_voc()
  175. {
  176. int count;
  177. voc_data = realloc(voc_data,voc_data_size*2);
  178. for (count = voc_data_size; count>=0; count--)
  179. { voc_data[count*2] = voc_data[count];
  180. voc_data[count*2+1] = voc_data[count];
  181. }
  182. voc_data_size *= 2;
  183. }
  184. #endif
  185.