AudioBuffer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. /**
  5. *
  6. * GE::GA AudioBuffer functionality
  7. * tuomo.hirvonen@digia.com
  8. *
  9. */
  10. #ifndef __GE_IGA_AUDIOBUFFER__
  11. #define __GE_IGA_AUDIOBUFFER__
  12. #include <stdio.h>
  13. #include "AudioInterfaces.h"
  14. namespace GF {
  15. class AudioBufferPlayInstance;
  16. class AudioBuffer; // forward declaration
  17. typedef AUDIO_SAMPLE_TYPE(*SAMPLE_FUNCTION_TYPE)(AudioBuffer *abuffer, int pos, int channel);
  18. class AudioBuffer { // container for a sound
  19. public:
  20. AudioBuffer();
  21. virtual ~AudioBuffer();
  22. static AudioBuffer *loadWav( const void *d, unsigned int dlength );
  23. static AudioBuffer* loadWav( FILE *wavFile ); // support for stdio
  24. static AudioBuffer* loadWav( const char *fname );
  25. void reallocate( int length );
  26. inline void* getRawData() { return m_data; }
  27. inline int getDataLength() { return m_dataLength; }
  28. inline int getBytesPerSample() { return (m_bitsPerSample>>3); }
  29. inline int getBitsPerSample() { return m_bitsPerSample; }
  30. inline int getSamplesPerSec() { return m_samplesPerSec; }
  31. inline short getNofChannels() { return m_nofChannels; }
  32. inline SAMPLE_FUNCTION_TYPE getSampleFunction() { return m_sampleFunction; }
  33. // static implementations of sample functions
  34. static AUDIO_SAMPLE_TYPE sampleFunction8bitMono( AudioBuffer *abuffer, int pos, int channel );
  35. static AUDIO_SAMPLE_TYPE sampleFunction16bitMono( AudioBuffer *abuffer, int pos, int channel );
  36. static AUDIO_SAMPLE_TYPE sampleFunction8bitStereo( AudioBuffer *abuffer, int pos, int channel );
  37. static AUDIO_SAMPLE_TYPE sampleFunction16bitStereo( AudioBuffer *abuffer, int pos, int channel );
  38. AudioBufferPlayInstance *playWithMixer( GF::AudioMixer &mixer );
  39. protected:
  40. SAMPLE_FUNCTION_TYPE m_sampleFunction;
  41. short m_nofChannels;
  42. void *m_data;
  43. int m_dataLength; // in bytes
  44. short m_bitsPerSample;
  45. bool m_signedData;
  46. int m_samplesPerSec;
  47. };
  48. class AudioBufferPlayInstance : public IAudioSource {
  49. public:
  50. AudioBufferPlayInstance();
  51. AudioBufferPlayInstance( AudioBuffer *start_playing );
  52. virtual ~AudioBufferPlayInstance();
  53. void playBuffer( AudioBuffer *startPlaying, float volume, float fixedSpeed, int loopTimes = 0 ); // looptimes -1 = loop forever
  54. void setSpeed( float speed );
  55. void setLeftVolume( float lvol );
  56. void setRightVolume( float rvol );
  57. inline void setLoopTimes( int ltimes ) { m_loopTimes = ltimes; }
  58. void stop();
  59. int pullAudio( AUDIO_SAMPLE_TYPE *target, int sampleCount );
  60. bool canBeDestroyed();
  61. bool isPlaying() { if (m_buffer) return true; else return false; }
  62. inline bool isFinished() { return m_finished; }
  63. inline bool destroyWhenFinished() { return m_destroyWhenFinished; }
  64. inline void setDestroyWhenFinished( bool set ) { m_destroyWhenFinished = set; }
  65. protected:
  66. int mixBlock( AudioBuffer *buffer, AUDIO_SAMPLE_TYPE *target, int samplesToMix );
  67. bool m_finished;
  68. bool m_destroyWhenFinished;
  69. int m_fixedPos;
  70. int m_fixedInc;
  71. int m_fixedLeftVolume;
  72. int m_fixedRightVolume;
  73. int m_fixedCenter;
  74. int m_loopTimes;
  75. AudioBuffer *m_buffer;
  76. };
  77. };
  78. #endif