WAVDatei.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "WAVDatei.h"
  2. #include <Bild.h>
  3. #include <DateiSystem.h>
  4. #include <mmsystem.h>
  5. #include <Text.h>
  6. bool istGleich(const char* a, const 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)) return 0;
  23. d.lese((char*)&kpf, sizeof(kpf));
  24. if (!istGleich(kpf.riff, "RIFF", 4)) return 0;
  25. if (!istGleich(kpf.wav, "WAVE", 4)) return 0;
  26. d.lese((char*)&fmt, sizeof(fmt));
  27. if (!istGleich(fmt.fmt, "fmt ", 4)) return 0;
  28. if (fmt.channels > 2 || fmt.channels < 1) return 0;
  29. if (fmt.tag != 1) return 0;
  30. if (fmt.bitsPerSample != 16) return 0;
  31. if (fmt.blockAlign * fmt.sampleRate != fmt.bytesPerSec) return 0;
  32. d.lese((char*)&dBeg, sizeof(dBeg));
  33. while (!istGleich(dBeg.data, "data", 4))
  34. {
  35. d.setLPosition(1 + d.getLPosition() - sizeof(dBeg), 0);
  36. d.lese((char*)&dBeg, sizeof(dBeg));
  37. }
  38. pos = (int)d.getLPosition();
  39. d.close();
  40. return 1;
  41. }
  42. // zum Speichern
  43. void WAVDatei::open()
  44. {
  45. d.open(Datei::Style::lesen);
  46. d.setLPosition(pos, 0);
  47. }
  48. int WAVDatei::getDaten(char* buffer, int län)
  49. {
  50. if (d.getLPosition() + län > d.getSize())
  51. län = (int)(d.getSize() - d.getLPosition());
  52. if (!län) return -1;
  53. d.lese(buffer, län);
  54. return län;
  55. }
  56. void WAVDatei::close()
  57. {
  58. d.close();
  59. }
  60. bool WAVDatei::istMono() const
  61. {
  62. return fmt.channels == 1;
  63. }
  64. int WAVDatei::getSampleRate() const
  65. {
  66. return fmt.sampleRate;
  67. }
  68. __int64 WAVDatei::getDatLength() const
  69. {
  70. return d.getSize() - pos;
  71. }