Getting something from Ref with -> "crashes"? the thing (?)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By QonDonion

so i wanted to generate a sound
and then

( Oscillator::playback is Ref< AudioGeneratorStreamPlayback > )

int emptys = Oscillator::playback->get_frames_available();

that thing →
grrrr >:(
does this

closes the thing!!! for no reasion!!! owwww!!!
no eror

testing with other functions

nope, still closes the thing!!! for no reasion!!! owwww!!!
no eror

MORE INFORMATION:
Header:

#ifndef define_deez
#define define_deez

#include <godot_cpp/classes/audio_stream_player2d.hpp>
#include <godot_cpp/classes/audio_stream_generator_playback.hpp>
#include <godot_cpp/classes/audio_stream_generator.hpp>
#include <iostream>
#include <vector>

using namespace godot;
using namespace std;

class Oscillator : public AudioStreamPlayer2D {
	GDCLASS(Oscillator, AudioStreamPlayer2D);

public:
	void init();
	void writeFrame();
	void writeWave();
	void genSine(float freq);
	//void genRamp(float freq);
	//void genSquare(float freq);
	//void genTri(float freq);
	void advanceFrame();
	void setTime(float delta);
	vector<vector<float>> getWaveData();

private:
	Ref<AudioStreamGeneratorPlayback> playback;
	vector<vector<float>> waveData;
	float interval;
	//float delta;
	float waveTime;
	AudioStreamGenerator stream;

protected:
	static void _bind_methods();
};

#endif

Source:

#include "Header1.h"
#include <godot_cpp/variant/utility_functions.hpp>
#include <godot_cpp/classes/audio_stream_player2d.hpp>
#include <godot_cpp/classes/audio_stream_generator.hpp>
#include <cmath>

using namespace godot;

void Oscillator::init() {
	//the things
	Oscillator::set_stream(&AudioStreamGenerator());
	Oscillator::playback = Oscillator::get_stream_playback();
	//Oscillator::playback.instantiate();
	//the wavw datae
	int buffer_size = Oscillator::stream.get_mix_rate() * Oscillator::stream.get_buffer_length();
	Oscillator::waveData.resize(buffer_size);
	for (int i = 0; i < buffer_size; i++) {
		Oscillator::waveData[i].resize(2);
	}
	//delete buffer_size;

	Oscillator::waveTime = 0.0;
	Oscillator::interval = 1 / Oscillator::stream.get_mix_rate();

	UtilityFunctions::print("mix rate: ", Oscillator::stream.get_mix_rate());
	UtilityFunctions::print("buffer length: ", Oscillator::stream.get_buffer_length());
}


void Oscillator::writeWave() {
	//WHYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY


	//Oscillator::playback->push_buffer(PackedVector2Array(Oscillator::waveData));
	//for (int i = 0; i < Oscillator::waveData.size(); i++) {
		//Oscillator::playback->push_frame(Vector2(Oscillator::waveData[i][0], Oscillator::waveData[i][1]));
		//UtilityFunctions::print("AAAAAAAAAA %i", Oscillator::waveData[i][0], Oscillator::waveData[i][1]);
	//}

	//var increment = pulse_hz / sample_hz
	//	var to_fill = playback.get_frames_available()
	//	while to_fill > 0:
	//playback.push_frame(Vector2.ONE * sin(phase * TAU)) # Audio frames are stereo.
	//	phase = fmod(phase + increment, 1.0)
	//	to_fill -= 1

	//int i = 0;
	//int emptys = Oscillator::playback->get_frames_available();

	Oscillator::playback->clear_buffer();
	//
	//int emptys = 100;
	//while (emptys > 0) {
			//Oscillator::playback->push_frame(Vector2(Oscillator::waveData[i][0], Oscillator::waveData[i][1]));
	//		UtilityFunctions::print("FRAME:::::::: WD D:PS SPS D     ", i);
	//		i++;
	//		emptys -= 1;
	//	}

}

void Oscillator::genSine(float freq) {
	float interval = Oscillator::stream.get_mix_rate()/freq;
	for (int i = 0; i < Oscillator::waveData.size(); i++) {
		Oscillator::waveData[i][0] = sin((Oscillator::waveTime)*Math_TAU);
		Oscillator::waveData[i][1] = sin((Oscillator::waveTime)*Math_TAU);

		Oscillator::waveTime += interval;
	}
}

void Oscillator::setTime(float delta) {
	Oscillator::waveTime = delta;
}
void Oscillator::advanceFrame() {
	Oscillator::waveTime += Oscillator::interval;
}

vector<vector<float>> Oscillator::getWaveData() {
	return Oscillator::waveData;
}


void Oscillator::_bind_methods() {
	ClassDB::bind_method(D_METHOD("init"), &Oscillator::init);
	ClassDB::bind_method(D_METHOD("writeWave"), &Oscillator::writeWave);
	ClassDB::bind_method(D_METHOD("genSine", "freq"), &Oscillator::genSine);
	ClassDB::bind_method(D_METHOD("setTime"), &Oscillator::setTime);
	//ClassDB::bind_method(D_METHOD("getWaveData"), &Oscillator::getWaveData);
}

Oscillator GDscript:

extends Oscillator


# Called when the node enters the scene tree for the first time.
func _ready():
	init()
	
	# Setting mix rate is only possible before play().
	play()
	# `_fill_buffer` must be called *after* setting `playback`,
	# as `fill_buffer` uses the `playback` member variable.
	genSine(440)
	writeWave()


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	genSine(440)
	play()
	#writeWave()

My Emotion:

:| what :/ guh :( wh <:( what why

EDITS:
edit1:
i have tried Oscillator::playback.instantiate(); but it still doesn’t the thing!!! for no reasion!!! owwww!!!
no eror
edit2:
what the
it seems like Oscillator::playback.instantiate(); also crashes it

Best guess is that Ref<AudioStreamGeneratorPlayback> playback isn’t initialised (that is, init hasn’t been called by the engine and something is calling writeWave) or that the AudioStreamPlayback object associated with the AudioStreamPlayer (Oscillator) hasn’t been assigned and is returning nullptr.

Confirm it has been assigned and if so, it should be an easy problem to track in the debugger.

spaceyjase | 2023-05-05 11:28

:bust_in_silhouette: Reply From: QonDonion

when:
https://forum.godotengine.org/152917/creepy-sine-wave-with-audiostreamgeneratorplayback