WAVDatei.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "WAVDatei.h"
  2. #include <Text.h>
  3. #include <mmsystem.h>
  4. #include <Bild.h>
  5. #include <DateiSystem.h>
  6. bool istGleich(char* a, char* b, int l)
  7. {
  8. bool ret = 1;
  9. for (; l > 0; --l, ++a, ++b)
  10. ret &= *a == *b;
  11. return ret;
  12. }
  13. // Inhalt der WAVDatei Klasse aus WAVDatei.h
  14. // Konstruktor
  15. WAVDatei::WAVDatei()
  16. : ReferenceCounter()
  17. {}
  18. // nicht constant
  19. bool WAVDatei::lade(const char* pfad)
  20. {
  21. d.setDatei(pfad);
  22. if (!d.open(Datei::Style::lesen))
  23. return 0;
  24. d.lese((char*)&kpf, sizeof(kpf));
  25. if (!istGleich(kpf.riff, "RIFF", 4))
  26. return 0;
  27. if (!istGleich(kpf.wav, "WAVE", 4))
  28. return 0;
  29. d.lese((char*)&fmt, sizeof(fmt));
  30. if (!istGleich(fmt.fmt, "fmt ", 4))
  31. return 0;
  32. if (fmt.channels > 2 || fmt.channels < 1)
  33. return 0;
  34. if (fmt.tag != 1)
  35. return 0;
  36. if (fmt.bitsPerSample != 16)
  37. return 0;
  38. if (fmt.blockAlign * fmt.sampleRate != fmt.bytesPerSec)
  39. return 0;
  40. d.lese((char*)&dBeg, sizeof(dBeg));
  41. while (!istGleich(dBeg.data, "data", 4))
  42. {
  43. d.setLPosition(1 + d.getLPosition() - sizeof(dBeg), 0);
  44. d.lese((char*)&dBeg, sizeof(dBeg));
  45. }
  46. pos = (int)d.getLPosition();
  47. d.close();
  48. return 1;
  49. }
  50. // zum Speichern
  51. void WAVDatei::open()
  52. {
  53. d.open(Datei::Style::lesen);
  54. d.setLPosition(pos, 0);
  55. }
  56. int WAVDatei::getDaten(char* buffer, int län)
  57. {
  58. if (d.getLPosition() + län > d.getSize())
  59. län = (int)(d.getSize() - d.getLPosition());
  60. if (!län)
  61. return -1;
  62. d.lese(buffer, län);
  63. return län;
  64. }
  65. void WAVDatei::close()
  66. {
  67. d.close();
  68. }
  69. bool WAVDatei::istMono() const
  70. {
  71. return fmt.channels == 1;
  72. }
  73. int WAVDatei::getSampleRate() const
  74. {
  75. return fmt.sampleRate;
  76. }
  77. __int64 WAVDatei::getDatLength() const
  78. {
  79. return d.getSize() - pos;
  80. }