説明なし

wavesound.h 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef WAVESOUND_H
  2. #define WAVESOUND_H
  3. #include <QtMultimedia/QAudioBuffer>
  4. #include <QtMultimedia/QAudioFormat>
  5. #include <QFile>
  6. #include <iostream>
  7. #include <QByteArray>
  8. using namespace std ;
  9. class WaveSound
  10. {
  11. private:
  12. QByteArray header;
  13. QAudioFormat format;
  14. QByteArray wavBuffer;
  15. QAudioBuffer *qab;
  16. public:
  17. WaveSound()
  18. {
  19. qab = NULL ;
  20. format.setSampleSize(16);
  21. format.setSampleRate(44100);
  22. format.setChannelCount(2);
  23. format.setCodec("audio/pcm");
  24. format.setByteOrder(QAudioFormat::LittleEndian);
  25. format.setSampleType(QAudioFormat::SignedInt);
  26. }
  27. QAudioBuffer::S16S * getAudioSamples(QString filename)
  28. {
  29. QFile inputFile;
  30. inputFile.setFileName(filename);
  31. inputFile.open(QIODevice::ReadOnly);
  32. header = inputFile.read(44) ; // Wav header file is normally 44 bytes
  33. wavBuffer = inputFile.readAll();
  34. inputFile.close();
  35. if(qab != NULL){
  36. delete qab ;
  37. }
  38. qab = new QAudioBuffer(wavBuffer, format);
  39. return qab->data<QAudioBuffer::S16S>();
  40. }
  41. void writeNewAudioFile(QString filename)
  42. {
  43. QByteArray outBuffer;
  44. QFile outputFile;
  45. outputFile.setFileName(filename);
  46. outputFile.open(QIODevice::WriteOnly);
  47. outBuffer.setRawData((char *) qab->data(), qab->byteCount() );
  48. outputFile.write(header);
  49. outputFile.write(outBuffer);
  50. outputFile.close();
  51. }
  52. int frameCount()
  53. {
  54. return qab->frameCount();
  55. }
  56. ~WaveSound(){
  57. if(qab != NULL){
  58. delete qab ;
  59. qab = NULL;
  60. }
  61. }
  62. };
  63. #endif // WAVESOUND_H