Up to date

This page is up to date for Godot 4.0. If you still find outdated information, please open an issue.

Custom AudioStreams

Introduction

AudioStream is the base class of all audio emitting objects. AudioStreamPlayer binds onto an AudioStream to emit PCM data into an AudioServer which manages audio drivers.

All audio resources require two audio based classes: AudioStream and AudioStreamPlayback. As a data container, AudioStream contains the resource and exposes itself to GDScript. AudioStream references its own internal custom AudioStreamPlayback which translates AudioStream into PCM data.

This guide assumes the reader knows how to create C++ modules. If not, refer to this guide Custom modules in C++.

References:

What for?

  • Binding external libraries (like Wwise, FMOD, etc).

  • Adding custom audio queues

  • Adding support for more audio formats

Create an AudioStream

An AudioStream consists of three components: data container, stream name, and an AudioStreamPlayback friend class generator. Audio data can be loaded in a number of ways such as with an internal counter for a tone generator, internal/external buffer, or a file reference.

Some AudioStreams need to be stateless such as objects loaded from ResourceLoader. ResourceLoader loads once and references the same object regardless how many times load is called on a specific resource. Therefore, playback state must be self-contained in AudioStreamPlayback.

  1. /* audiostream_mytone.h */
  2. #include "core/reference.h"
  3. #include "core/resource.h"
  4. #include "servers/audio/audio_stream.h"
  5. class AudioStreamMyTone : public AudioStream {
  6. GDCLASS(AudioStreamMyTone, AudioStream)
  7. private:
  8. friend class AudioStreamPlaybackMyTone;
  9. uint64_t pos;
  10. int mix_rate;
  11. bool stereo;
  12. int hz;
  13. public:
  14. void reset();
  15. void set_position(uint64_t pos);
  16. virtual Ref<AudioStreamPlayback> instance_playback();
  17. virtual String get_stream_name() const;
  18. void gen_tone(int16_t *pcm_buf, int size);
  19. virtual float get_length() const { return 0; } // if supported, otherwise return 0
  20. AudioStreamMyTone();
  21. protected:
  22. static void _bind_methods();
  23. };
  1. /* audiostream_mytone.cpp */
  2. #include "audiostream_mytone.h"
  3. AudioStreamMyTone::AudioStreamMyTone()
  4. : mix_rate(44100), stereo(false), hz(639) {
  5. }
  6. Ref<AudioStreamPlayback> AudioStreamMyTone::instance_playback() {
  7. Ref<AudioStreamPlaybackMyTone> talking_tree;
  8. talking_tree.instantiate();
  9. talking_tree->base = Ref<AudioStreamMyTone>(this);
  10. return talking_tree;
  11. }
  12. String AudioStreamMyTone::get_stream_name() const {
  13. return "MyTone";
  14. }
  15. void AudioStreamMyTone::reset() {
  16. set_position(0);
  17. }
  18. void AudioStreamMyTone::set_position(uint64_t p) {
  19. pos = p;
  20. }
  21. void AudioStreamMyTone::gen_tone(int16_t *pcm_buf, int size) {
  22. for (int i = 0; i < size; i++) {
  23. pcm_buf[i] = 32767.0 * sin(2.0 * Math_PI * double(pos + i) / (double(mix_rate) / double(hz)));
  24. }
  25. pos += size;
  26. }
  27. void AudioStreamMyTone::_bind_methods() {
  28. ClassDB::bind_method(D_METHOD("reset"), &AudioStreamMyTone::reset);
  29. ClassDB::bind_method(D_METHOD("get_stream_name"), &AudioStreamMyTone::get_stream_name);
  30. }

References:

Create an AudioStreamPlayback

AudioStreamPlayer uses mix callback to obtain PCM data. The callback must match sample rate and fill the buffer.

Since AudioStreamPlayback is controlled by the audio thread, i/o and dynamic memory allocation are forbidden.

  1. /* audiostreamplayer_mytone.h */
  2. #include "core/reference.h"
  3. #include "core/resource.h"
  4. #include "servers/audio/audio_stream.h"
  5. class AudioStreamPlaybackMyTone : public AudioStreamPlayback {
  6. GDCLASS(AudioStreamPlaybackMyTone, AudioStreamPlayback)
  7. friend class AudioStreamMyTone;
  8. private:
  9. enum {
  10. PCM_BUFFER_SIZE = 4096
  11. };
  12. enum {
  13. MIX_FRAC_BITS = 13,
  14. MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
  15. MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
  16. };
  17. void *pcm_buffer;
  18. Ref<AudioStreamMyTone> base;
  19. bool active;
  20. public:
  21. virtual void start(float p_from_pos = 0.0);
  22. virtual void stop();
  23. virtual bool is_playing() const;
  24. virtual int get_loop_count() const; // times it looped
  25. virtual float get_playback_position() const;
  26. virtual void seek(float p_time);
  27. virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
  28. virtual float get_length() const; // if supported, otherwise return 0
  29. AudioStreamPlaybackMyTone();
  30. ~AudioStreamPlaybackMyTone();
  31. };
  1. /* audiostreamplayer_mytone.cpp */
  2. #include "audiostreamplayer_mytone.h"
  3. #include "core/math/math_funcs.h"
  4. #include "core/print_string.h"
  5. AudioStreamPlaybackMyTone::AudioStreamPlaybackMyTone()
  6. : active(false) {
  7. AudioServer::get_singleton()->lock();
  8. pcm_buffer = AudioServer::get_singleton()->audio_data_alloc(PCM_BUFFER_SIZE);
  9. zeromem(pcm_buffer, PCM_BUFFER_SIZE);
  10. AudioServer::get_singleton()->unlock();
  11. }
  12. AudioStreamPlaybackMyTone::~AudioStreamPlaybackMyTone() {
  13. if(pcm_buffer) {
  14. AudioServer::get_singleton()->audio_data_free(pcm_buffer);
  15. pcm_buffer = NULL;
  16. }
  17. }
  18. void AudioStreamPlaybackMyTone::stop() {
  19. active = false;
  20. base->reset();
  21. }
  22. void AudioStreamPlaybackMyTone::start(float p_from_pos) {
  23. seek(p_from_pos);
  24. active = true;
  25. }
  26. void AudioStreamPlaybackMyTone::seek(float p_time) {
  27. float max = get_length();
  28. if (p_time < 0) {
  29. p_time = 0;
  30. }
  31. base->set_position(uint64_t(p_time * base->mix_rate) << MIX_FRAC_BITS);
  32. }
  33. void AudioStreamPlaybackMyTone::mix(AudioFrame *p_buffer, float p_rate, int p_frames) {
  34. ERR_FAIL_COND(!active);
  35. if (!active) {
  36. return;
  37. }
  38. zeromem(pcm_buffer, PCM_BUFFER_SIZE);
  39. int16_t *buf = (int16_t *)pcm_buffer;
  40. base->gen_tone(buf, p_frames);
  41. for(int i = 0; i < p_frames; i++) {
  42. float sample = float(buf[i]) / 32767.0;
  43. p_buffer[i] = AudioFrame(sample, sample);
  44. }
  45. }
  46. int AudioStreamPlaybackMyTone::get_loop_count() const {
  47. return 0;
  48. }
  49. float AudioStreamPlaybackMyTone::get_playback_position() const {
  50. return 0.0;
  51. }
  52. float AudioStreamPlaybackMyTone::get_length() const {
  53. return 0.0;
  54. }
  55. bool AudioStreamPlaybackMyTone::is_playing() const {
  56. return active;
  57. }

Resampling

Godot’s AudioServer currently uses 44100 Hz sample rate. When other sample rates are needed such as 48000, either provide one or use AudioStreamPlaybackResampled. Godot provides cubic interpolation for audio resampling.

Instead of overloading mix, AudioStreamPlaybackResampled uses _mix_internal to query AudioFrames and get_stream_sampling_rate to query current mix rate.

  1. #include "core/reference.h"
  2. #include "core/resource.h"
  3. #include "servers/audio/audio_stream.h"
  4. class AudioStreamMyToneResampled;
  5. class AudioStreamPlaybackResampledMyTone : public AudioStreamPlaybackResampled {
  6. GDCLASS(AudioStreamPlaybackResampledMyTone, AudioStreamPlaybackResampled)
  7. friend class AudioStreamMyToneResampled;
  8. private:
  9. enum {
  10. PCM_BUFFER_SIZE = 4096
  11. };
  12. enum {
  13. MIX_FRAC_BITS = 13,
  14. MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
  15. MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
  16. };
  17. void *pcm_buffer;
  18. Ref<AudioStreamMyToneResampled> base;
  19. bool active;
  20. protected:
  21. virtual void _mix_internal(AudioFrame *p_buffer, int p_frames);
  22. public:
  23. virtual void start(float p_from_pos = 0.0);
  24. virtual void stop();
  25. virtual bool is_playing() const;
  26. virtual int get_loop_count() const; // times it looped
  27. virtual float get_playback_position() const;
  28. virtual void seek(float p_time);
  29. virtual float get_length() const; // if supported, otherwise return 0
  30. virtual float get_stream_sampling_rate();
  31. AudioStreamPlaybackResampledMyTone();
  32. ~AudioStreamPlaybackResampledMyTone();
  33. };
  1. #include "mytone_audiostream_resampled.h"
  2. #include "core/math/math_funcs.h"
  3. #include "core/print_string.h"
  4. AudioStreamPlaybackResampledMyTone::AudioStreamPlaybackResampledMyTone()
  5. : active(false) {
  6. AudioServer::get_singleton()->lock();
  7. pcm_buffer = AudioServer::get_singleton()->audio_data_alloc(PCM_BUFFER_SIZE);
  8. zeromem(pcm_buffer, PCM_BUFFER_SIZE);
  9. AudioServer::get_singleton()->unlock();
  10. }
  11. AudioStreamPlaybackResampledMyTone::~AudioStreamPlaybackResampledMyTone() {
  12. if (pcm_buffer) {
  13. AudioServer::get_singleton()->audio_data_free(pcm_buffer);
  14. pcm_buffer = NULL;
  15. }
  16. }
  17. void AudioStreamPlaybackResampledMyTone::stop() {
  18. active = false;
  19. base->reset();
  20. }
  21. void AudioStreamPlaybackResampledMyTone::start(float p_from_pos) {
  22. seek(p_from_pos);
  23. active = true;
  24. }
  25. void AudioStreamPlaybackResampledMyTone::seek(float p_time) {
  26. float max = get_length();
  27. if (p_time < 0) {
  28. p_time = 0;
  29. }
  30. base->set_position(uint64_t(p_time * base->mix_rate) << MIX_FRAC_BITS);
  31. }
  32. void AudioStreamPlaybackResampledMyTone::_mix_internal(AudioFrame *p_buffer, int p_frames) {
  33. ERR_FAIL_COND(!active);
  34. if (!active) {
  35. return;
  36. }
  37. zeromem(pcm_buffer, PCM_BUFFER_SIZE);
  38. int16_t *buf = (int16_t *)pcm_buffer;
  39. base->gen_tone(buf, p_frames);
  40. for(int i = 0; i < p_frames; i++) {
  41. float sample = float(buf[i]) / 32767.0;
  42. p_buffer[i] = AudioFrame(sample, sample);
  43. }
  44. }
  45. float AudioStreamPlaybackResampledMyTone::get_stream_sampling_rate() {
  46. return float(base->mix_rate);
  47. }
  48. int AudioStreamPlaybackResampledMyTone::get_loop_count() const {
  49. return 0;
  50. }
  51. float AudioStreamPlaybackResampledMyTone::get_playback_position() const {
  52. return 0.0;
  53. }
  54. float AudioStreamPlaybackResampledMyTone::get_length() const {
  55. return 0.0;
  56. }
  57. bool AudioStreamPlaybackResampledMyTone::is_playing() const {
  58. return active;
  59. }

References: