GSLDatei.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #include "GSLDatei.h"
  2. #ifdef WIN32
  3. #include "Sound.h"
  4. #else
  5. #include "Sound.h"
  6. #endif
  7. using namespace GSL;
  8. // Inhalt der GSLDatei Klasse aus GSLDatei.h
  9. // Konstruktor
  10. GSLDatei::GSLDatei()
  11. {
  12. sounds = new Array< SoundKopf >();
  13. pfad = new Text();
  14. InitializeCriticalSection( &cs );
  15. ref = 1;
  16. }
  17. // Destruktor
  18. GSLDatei::~GSLDatei()
  19. {
  20. sounds->release();
  21. pfad->release();
  22. DeleteCriticalSection( &cs );
  23. }
  24. // Datei open
  25. void GSLDatei::setDatei( Framework::Text *txt )
  26. {
  27. EnterCriticalSection( &cs );
  28. pfad->setText( txt );
  29. LeaveCriticalSection( &cs );
  30. }
  31. void GSLDatei::setDatei( char *txt )
  32. {
  33. EnterCriticalSection( &cs );
  34. pfad->setText( txt );
  35. LeaveCriticalSection( &cs );
  36. }
  37. bool GSLDatei::leseDaten()
  38. {
  39. EnterCriticalSection( &cs );
  40. sounds->leeren();
  41. Datei d;
  42. d.setDatei( *pfad );
  43. if( !d.getSize() )
  44. return 1;
  45. if( !d.open( Datei::Style::lesen ) )
  46. {
  47. LeaveCriticalSection( &cs );
  48. return 0;
  49. }
  50. unsigned char sAnz = 0;
  51. d.lese( (char*)&sAnz, 1 );
  52. // Datei Sound - Köpfe einlesen
  53. for( int i = 0; i < sAnz; i++ )
  54. {
  55. SoundKopf kpf;
  56. bool bit = 0;
  57. char len = 0;
  58. // Namenslänge lesen
  59. for( int i = 0; i < 4; i++ )
  60. {
  61. d.getNextBit( bit );
  62. len = (char)( len | ( ( (char)bit << ( 3 - i ) ) & ( 1 << ( 3 - i ) ) ) );
  63. }
  64. len &= 15;
  65. // Name lesen
  66. for( int i = 0; i < len; i++ )
  67. {
  68. char zeichen = 0;
  69. for( int j = 0; j < 5; j++ )
  70. {
  71. d.getNextBit( bit );
  72. zeichen = (char)( zeichen | ( ( (char)bit << ( 4 - j ) ) & ( 1 << ( 4 - j ) ) ) );
  73. }
  74. zeichen &= 31;
  75. if( zeichen == 27 )
  76. zeichen = 'ü';
  77. if( zeichen == 28 )
  78. zeichen = 'ö';
  79. if( zeichen == 29 )
  80. zeichen = 'ä';
  81. if( zeichen == 30 )
  82. zeichen = 'ß';
  83. if( zeichen == 31 )
  84. zeichen = '.';
  85. if( zeichen < 27 )
  86. zeichen = (char)( zeichen + 'a' );
  87. kpf.name.append( &zeichen, 1 );
  88. } // Chanel Anzahl lesen
  89. d.getNextBit( bit );
  90. kpf.channels = (char)( bit + 1 );
  91. // Sample Rate lesen
  92. d.lese( (char*)&kpf.sampleRate, 4 );
  93. // Position des Sounds in der Datei lesen
  94. d.lese( (char*)&kpf.datPos, 8 );
  95. // Länge des Sounds lesen
  96. int sLen = 0;
  97. d.lese( (char*)&sLen, 4 );
  98. kpf.datEnd = kpf.datPos + sLen;
  99. kpf.pfad = pfad->getText();
  100. sounds->add( kpf );
  101. }
  102. d.close();
  103. LeaveCriticalSection( &cs );
  104. return 1;
  105. }
  106. int GSLDatei::getSoundAnzahl()
  107. {
  108. return sounds->getEintragAnzahl();
  109. }
  110. Text *GSLDatei::getSoundName( int num )
  111. {
  112. if( !sounds->hat( num ) )
  113. return 0;
  114. return new Text( sounds->get( num ).name );
  115. }
  116. // Laden
  117. GSLSoundV *GSLDatei::getSound( Framework::Text *name )
  118. {
  119. GSLSoundV *ret = getSound( *name );
  120. name->release();
  121. return ret;
  122. }
  123. GSLSoundV *GSLDatei::getSound( char *name )
  124. {
  125. EnterCriticalSection( &cs );
  126. int anz = sounds->getEintragAnzahl();
  127. for( int i = 0; i < anz; i++ )
  128. {
  129. if( sounds->get( i ).name.istGleich( name ) )
  130. {
  131. LeaveCriticalSection( &cs );
  132. return new GSLSound( sounds->get( i ) );
  133. }
  134. }
  135. LeaveCriticalSection( &cs );
  136. return 0;
  137. }
  138. // Speichern
  139. bool GSLDatei::speicherSound( GSLSoundV *zSound, Framework::Text *name )
  140. {
  141. bool ret = speicherSound( zSound, *name );
  142. name->release();
  143. return ret;
  144. }
  145. bool GSLDatei::speicherSound( GSLSoundV *zSound, char *name )
  146. {
  147. Text kName;
  148. for( char *c = name; *c; ++c )
  149. {
  150. if( *c >= 'A' && *c <= 'Z' )
  151. kName.append( (char)( *c + 32 ) );
  152. if( *c >= 'a' && *c <= 'z' )
  153. kName.append( *c );
  154. if( *c == 'Ü' || *c == 'ü' )
  155. kName.append( 'ü' );
  156. if( *c == 'Ö' || *c == 'ö' )
  157. kName.append( 'ö' );
  158. if( *c == 'Ä' || *c == 'ä' )
  159. kName.append( 'ä' );
  160. if( *c == 'ß' )
  161. kName.append( 'ß' );
  162. if( *c == '.' )
  163. kName.append( '.' );
  164. if( kName.getLength() == 15 )
  165. break;
  166. }
  167. name = kName;
  168. EnterCriticalSection( &cs );
  169. // Prüfen ob bereits vorhanden
  170. int anz = sounds->getEintragAnzahl();
  171. for( int i = 0; i < anz; i++ )
  172. {
  173. if( sounds->get( i ).name.istGleich( name ) )
  174. {
  175. LeaveCriticalSection( &cs );
  176. return 0;
  177. }
  178. }
  179. Text tmpPf = pfad->getText();
  180. tmpPf += "_";
  181. GetFreePfad( &tmpPf );
  182. Datei tmp;
  183. tmp.setDatei( tmpPf );
  184. tmp.erstellen();
  185. if( !tmp.open( Datei::Style::schreiben ) )
  186. {
  187. LeaveCriticalSection( &cs );
  188. return 0;
  189. }
  190. char *buffer = new char[ 0x4000 ];
  191. // Neuen Sound in Temporäre Datei schreiben
  192. zSound->open();
  193. while( 1 )
  194. {
  195. int len = 0;
  196. if( ( len = zSound->getDaten( buffer, 0x4000 ) ) < 0 )
  197. break;
  198. tmp.schreibe( buffer, len );
  199. }
  200. zSound->close();
  201. delete buffer;
  202. tmp.close();
  203. int hLen = 1;
  204. unsigned char sAnz = (unsigned char)sounds->getEintragAnzahl();
  205. // Sound Kopf Länge errechnen
  206. for( int i = 0; i < sAnz; i++ )
  207. {
  208. SoundKopf kpf = sounds->get( i );
  209. hLen += ( 140 + 5 * kpf.name.getLength() ) / 8;
  210. }
  211. Datei alt;
  212. alt.setDatei( *pfad );
  213. // Sound Kopf Erstellen
  214. SoundKopf kpf;
  215. kpf.channels = (char)( !zSound->istMono() + 1 );
  216. kpf.name = name;
  217. kpf.pfad = pfad->getText();
  218. kpf.sampleRate = zSound->getSampleRate();
  219. kpf.datPos = ( alt.getSize() <= 0 ? 1 : alt.getSize() );
  220. kpf.datEnd = tmp.getSize() + kpf.datPos;
  221. sounds->add( kpf );
  222. Text neuPf = pfad->getText();
  223. neuPf += "_";
  224. GetFreePfad( &neuPf );
  225. Datei neu;
  226. neu.setDatei( neuPf );
  227. neu.erstellen();
  228. if( !neu.open( Datei::Style::schreiben ) )
  229. {
  230. sounds->remove( sAnz );
  231. tmp.remove();
  232. LeaveCriticalSection( &cs );
  233. return 0;
  234. }
  235. sAnz = (unsigned char)sounds->getEintragAnzahl();
  236. neu.schreibe( (char*)&sAnz, 1 );
  237. // Sound Köpfe speichern
  238. for( int i = 0; i < sAnz; i++ )
  239. {
  240. SoundKopf k = sounds->get( i );
  241. k.datPos += ( 140 + 5 * kpf.name.getLength() ) / 8;
  242. k.datEnd += ( 140 + 5 * kpf.name.getLength() ) / 8;
  243. int l = k.name.getLength();
  244. // Namenslänge speichern
  245. neu.setNextBit( ( l & 8 ) != 0 );
  246. neu.setNextBit( ( l & 4 ) != 0 );
  247. neu.setNextBit( ( l & 2 ) != 0 );
  248. neu.setNextBit( ( l & 1 ) != 0 );
  249. for( int j = 0; j < l; j++ )
  250. { // Name speichern
  251. char c = k.name.getText()[ j ];
  252. if( c == 'ü' )
  253. c = 27;
  254. if( c == 'ö' )
  255. c = 28;
  256. if( c == 'ä' )
  257. c = 29;
  258. if( c == 'ß' )
  259. c = 30;
  260. if( c == '.' )
  261. c = 31;
  262. if( c >= 'a' && c <= 'z' )
  263. c = (char)( c - 'a' );
  264. neu.setNextBit( ( c & 16 ) != 0 );
  265. neu.setNextBit( ( c & 8 ) != 0 );
  266. neu.setNextBit( ( c & 4 ) != 0 );
  267. neu.setNextBit( ( c & 2 ) != 0 );
  268. neu.setNextBit( ( c & 1 ) != 0 );
  269. } // Chanels speichern
  270. neu.setNextBit( ( k.channels - 1 ) == 1 );
  271. // Sample Rate speichern
  272. neu.schreibe( (char*)&k.sampleRate, 4 );
  273. // Datei Position schreiben
  274. neu.schreibe( (char*)&k.datPos, 8 );
  275. // Länge des Sounds Speichern
  276. int sLen = (int)( k.datEnd - k.datPos );
  277. neu.schreibe( (char*)&sLen, 4 );
  278. }
  279. // alte Sounds kopieren
  280. char *byte = new char[ 2048 ];
  281. if( sAnz > 1 )
  282. {
  283. if( !alt.open( Datei::Style::lesen ) )
  284. {
  285. tmp.remove();
  286. neu.close();
  287. neu.remove();
  288. sounds->remove( sAnz );
  289. LeaveCriticalSection( &cs );
  290. return 0;
  291. }
  292. alt.setLPosition( hLen, 0 );
  293. for( __int64 i = hLen; i < alt.getSize(); )
  294. {
  295. int l = 2048;
  296. if( l > alt.getSize() - i )
  297. l = (int)( alt.getSize() - i );
  298. alt.lese( byte, l );
  299. neu.schreibe( byte, l );
  300. i += l;
  301. }
  302. alt.close();
  303. }
  304. // Neuen Sound kopieren
  305. if( !tmp.open( Datei::Style::lesen ) )
  306. {
  307. tmp.remove();
  308. neu.close();
  309. neu.remove();
  310. sounds->remove( sAnz );
  311. LeaveCriticalSection( &cs );
  312. return 0;
  313. }
  314. for( int i = 0; i < (int)tmp.getSize(); )
  315. {
  316. int l = 2048;
  317. if( l > tmp.getSize() - i )
  318. l = (int)( tmp.getSize() - i );
  319. tmp.lese( byte, l );
  320. neu.schreibe( byte, l );
  321. i += l;
  322. }
  323. delete[] byte;
  324. tmp.close();
  325. neu.close();
  326. // Dateien Umbenennen und remove
  327. tmp.remove();
  328. alt.remove();
  329. neu.umbenennen( *pfad );
  330. leseDaten();
  331. LeaveCriticalSection( &cs );
  332. return 1;
  333. }
  334. // Löschen
  335. bool GSLDatei::removeSound( Framework::Text *name )
  336. {
  337. bool ret = removeSound( *name );
  338. name->release();
  339. return ret;
  340. }
  341. bool GSLDatei::removeSound( char *name )
  342. {
  343. EnterCriticalSection( &cs );
  344. // Prüfen ob vorhanden
  345. int anz = sounds->getEintragAnzahl();
  346. int num = -1;
  347. for( int i = 0; i < anz; i++ )
  348. {
  349. if( sounds->get( i ).name.istGleich( name ) )
  350. {
  351. num = i;
  352. break;
  353. }
  354. }
  355. if( num < 0 )
  356. {
  357. LeaveCriticalSection( &cs );
  358. return 1;
  359. }
  360. Text neuPf = pfad->getText();
  361. neuPf += "_";
  362. GetFreePfad( &neuPf );
  363. Datei neu;
  364. neu.setDatei( neuPf );
  365. neu.erstellen();
  366. if( !neu.open( Datei::Style::schreiben ) )
  367. {
  368. LeaveCriticalSection( &cs );
  369. return 0;
  370. }
  371. SoundKopf kpf = sounds->get( num );
  372. unsigned char sAnz = (unsigned char)( anz - 1 );
  373. neu.schreibe( (char*)&sAnz, 1 );
  374. // Sound Köpfe speichern
  375. for( int i = 0; i <= sAnz; i++ )
  376. {
  377. if( i == num )
  378. continue;
  379. SoundKopf k = sounds->get( i );
  380. k.datPos -= ( 140 + 5 * kpf.name.getLength() ) / 8;
  381. k.datEnd -= ( 140 + 5 * kpf.name.getLength() ) / 8;
  382. if( i > num )
  383. {
  384. k.datPos -= kpf.datEnd - kpf.datPos;
  385. k.datEnd -= kpf.datEnd - kpf.datPos;
  386. }
  387. int l = k.name.getLength();
  388. // Namenslänge speichern
  389. neu.setNextBit( ( l & 8 ) != 0 );
  390. neu.setNextBit( ( l & 4 ) != 0 );
  391. neu.setNextBit( ( l & 2 ) != 0 );
  392. neu.setNextBit( ( l & 1 ) != 0 );
  393. for( int j = 0; j < l; j++ )
  394. { // Name speichern
  395. char c = k.name.getText()[ j ];
  396. if( c == 'ü' )
  397. c = 27;
  398. if( c == 'ö' )
  399. c = 28;
  400. if( c == 'ä' )
  401. c = 29;
  402. if( c == 'ß' )
  403. c = 30;
  404. if( c == '.' )
  405. c = 31;
  406. if( c >= 'a' && c <= 'z' )
  407. c = (char)( c - 'a' );
  408. neu.setNextBit( ( c & 16 ) != 0 );
  409. neu.setNextBit( ( c & 8 ) != 0 );
  410. neu.setNextBit( ( c & 4 ) != 0 );
  411. neu.setNextBit( ( c & 2 ) != 0 );
  412. neu.setNextBit( ( c & 1 ) != 0 );
  413. } // Chanels speichern
  414. neu.setNextBit( ( k.channels - 1 ) == 1 );
  415. // Sample Rate speichern
  416. neu.schreibe( (char*)&k.sampleRate, 4 );
  417. // Datei Position schreiben
  418. neu.schreibe( (char*)&k.datPos, 8 );
  419. // Länge des Sounds Speichern
  420. int sLen = (int)( k.datEnd - k.datPos );
  421. neu.schreibe( (char*)&sLen, 4 );
  422. }
  423. // Alte Sounds kopieren
  424. Datei alt;
  425. alt.setDatei( *pfad );
  426. if( sAnz )
  427. {
  428. if( !alt.open( Datei::Style::lesen ) )
  429. {
  430. neu.close();
  431. neu.remove();
  432. LeaveCriticalSection( &cs );
  433. return 0;
  434. }
  435. alt.setLPosition( neu.getSPosition() + ( 140 + 5 * kpf.name.getLength() ) / 8, 0 );
  436. char *byte = new char[ 2048 ];
  437. for( __int64 i = alt.getLPosition(); i < kpf.datPos; )
  438. {
  439. int l = 2048;
  440. if( l > kpf.datPos - i )
  441. l = (int)( kpf.datPos - i );
  442. alt.lese( byte, l );
  443. neu.schreibe( byte, l );
  444. i += l;
  445. }
  446. alt.setLPosition( kpf.datEnd, 0 );
  447. for( __int64 i = alt.getLPosition(); i < alt.getSize(); )
  448. {
  449. int l = 2048;
  450. if( l > alt.getSize() - i )
  451. l = (int)( alt.getSize() - i );
  452. alt.lese( byte, l );
  453. neu.schreibe( byte, l );
  454. i += l;
  455. }
  456. delete[] byte;
  457. alt.close();
  458. }
  459. neu.close();
  460. // Dateien Umbenennen und remove
  461. alt.remove();
  462. neu.umbenennen( *pfad );
  463. leseDaten();
  464. LeaveCriticalSection( &cs );
  465. return 1;
  466. }
  467. // Reference Counting
  468. GSLDateiV *GSLDatei::getThis()
  469. {
  470. ref++;
  471. return this;
  472. }
  473. GSLDateiV *GSLDatei::release()
  474. {
  475. ref--;
  476. if( !ref )
  477. delete this;
  478. return 0;
  479. }