Sound Stream.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /******************************************************************************
  2. Use 'SoundCallback' to play dynamically generated sound data.
  3. Use 'SoundStream' to manually decode audio streams.
  4. /******************************************************************************/
  5. enum SOUND_CODEC : Byte
  6. {
  7. SOUND_NONE , // none
  8. SOUND_WAV , // Waveform
  9. SOUND_FLAC , // Free Lossless Audio Codec
  10. SOUND_OGG_VORBIS , // Ogg Vorbis
  11. SOUND_OGG_OPUS , // Ogg Opus
  12. SOUND_WEBM_VORBIS, // WebM Vorbis
  13. SOUND_WEBM_OPUS , // WebM Opus
  14. SOUND_SND_VORBIS , // Esenthel Sound Vorbis
  15. SOUND_SND_OPUS , // Esenthel Sound Opus
  16. SOUND_MP3 , // MPEG Layer III
  17. SOUND_AAC , // Advanced Audio Coding
  18. SOUND_DYNAMIC , // data is provided dynamically from user provided 'SoundCallback'
  19. SOUND_NUM , // number of sound codecs
  20. };
  21. CChar8* CodecName(SOUND_CODEC codec); // get codec name
  22. #if EE_PRIVATE
  23. enum SND_CODEC : Byte
  24. {
  25. SND_RAW_16, // Raw 16-bit
  26. SND_VORBIS,
  27. SND_OPUS ,
  28. };
  29. #endif
  30. /******************************************************************************/
  31. const_mem_addr struct SoundCallback // Sound Callback used to dynamically create sound data !! must be stored in constant memory address !! this object is passed on to functions which store pointer to it, therefore it must be stored in a constant memory address
  32. {
  33. virtual Bool create(Byte &bits, Byte &channels, Int &frequency, Long &size, Int &bit_rate)=NULL; // 'create' will be called upon creation of the sound, you should override this method and inside it fill all of the parameters, 'bits'=number of bits per sample (use 8 for 8-bit samples and 16 for 16-bit samples), 'channels'=number of channels (valid values are '1'=mono, '2'=stereo), 'frequency'=number of samples per second (valid values are 1..192000, recommended value is 44100 or 48000), 'size'=total size of the uncompressed sound data in bytes (use -1 if the size is unknown), 'bit_rate'=this parameter is optional, it specifies the average number of compressed data bytes needed to fill a 1 second of uncompressed data (use -1 if unknown), return false on fail, warning: this may get called on secondary thread
  34. virtual Bool raw (Long raw ) {return true;} // 'raw' will be called when the sound is being requested to jump to specified position, the position is specified in raw bytes of uncompressed data, return false on fail, warning: this may get called on secondary thread
  35. virtual Int set (Ptr data, Int size)=NULL; // 'set' will be called when the sound is being requested to fill the 'data' buffer with raw uncompressed sound data of 'size' length, you should not write more data than requested 'size', return the number of bytes written or -1 on fail, warning: this may get called on secondary thread
  36. virtual void del ( ) {} // 'del' will be called when the sound is being destroyed, and this callback will no longer be accessed by it, warning: this may get called on secondary thread
  37. };
  38. /******************************************************************************/
  39. const_mem_addr struct SoundStream // can be moved however 'memAddressChanged' needs to be called afterwards
  40. {
  41. // get
  42. Int block ()C {return _par.block;} // bytes*channels
  43. Int bytes ()C {return _par.bytes;}
  44. Int bits ()C {return _par.bits();}
  45. Int channels ()C {return _par.channels;}
  46. Int frequency()C {return _par.frequency;}
  47. Int bitRate ()C {return _par.bit_rate;}
  48. Long size ()C {return _par.size;}
  49. Long samples ()C {return _par.samples();}
  50. Flt length ()C {return _par.length();}
  51. SOUND_CODEC codec ()C {return _codec;}
  52. CChar8* codecName()C {return CodecName(_codec);} // get codec name
  53. // manage
  54. void del ( ); // delete manually
  55. Bool create( C Str &name ); // create from file
  56. Bool create( C UID &id ); // create from file
  57. Bool create(const_mem_addr SoundCallback &callback); // create from sound callback
  58. Bool sample(Long sample); // set stream position, false on fail
  59. Long pos ()C {return _pos;} Bool pos (Long pos ); // get/set stream position, false on fail
  60. Long left()C {return size()-_pos;} // get bytes left to process in the stream
  61. Int set(Ptr data, Int size); // decode stream into 'data' buffer up to 'size' number of bytes, this method returns the actual number of bytes written (can be less than 'size', or <=0 on error)
  62. void fastSeek(); // if allow fast but not precise seeking, default=disabled
  63. void memAddressChanged(); // call this if 'SoundStream' object was moved to another memory address
  64. SoundStream();
  65. ~SoundStream() {del();}
  66. #if !EE_PRIVATE
  67. private:
  68. #endif
  69. struct Params // parameters
  70. {
  71. Byte bytes, channels, block;
  72. Int frequency, bit_rate;
  73. Long size;
  74. void zero() {bytes=channels=block=0; frequency=bit_rate=0; size=0;}
  75. Int bits ()C {return bytes*8;}
  76. Long samples()C {if(Int b=block)return size/b; return 0;}
  77. Flt length ()C; // get length in seconds
  78. };
  79. SOUND_CODEC _codec;
  80. union
  81. {
  82. UInt _raw_ofs;
  83. Ptr _extra;
  84. };
  85. Long _pos;
  86. File _f;
  87. Params _par;
  88. SoundCallback *_callback;
  89. #if EE_PRIVATE
  90. Bool open(C Str &name);
  91. #endif
  92. NO_COPY_CONSTRUCTOR(SoundStream);
  93. };
  94. /******************************************************************************/
  95. struct SoundHeader
  96. {
  97. SOUND_CODEC codec;
  98. Byte bytes, bits, channels;
  99. Int frequency, bit_rate;
  100. Long size;
  101. Flt length;
  102. CChar8* codecName()C; // get codec name
  103. // io
  104. Bool load(C Str &name);
  105. void zero();
  106. SoundHeader() {zero();}
  107. };
  108. /******************************************************************************/
  109. Bool SaveWavHeader(File &f, Int bits, Int channels, Int frequency, UInt size); // 'bits'=number of bits per sample (use 8 for 8-bit samples or 16 for 16-bit samples), 'channels'=number of channels (use 1 for mono and 2 for stereo), 'frequency'=sample rate, 'size'=size of audio data in bytes, after writing this header to a file you can store the raw audio data
  110. /******************************************************************************/
  111. #if EE_PRIVATE
  112. Bool SaveSndHeader(File &f, SND_CODEC codec, Int channels, Int frequency, Long samples); // 'channels'=number of channels (use 1 for mono and 2 for stereo), 'frequency'=sample rate, 'samples'=number of audio samples
  113. void InitStream();
  114. void ShutStream();
  115. #endif
  116. /******************************************************************************/