PrevUpHomeNext

Haiku c++: BSoundPlayer


BSoundPlayer of Haiku c++ audio api.

Kit: Haiku media kit.

Read BeBook

Constructor:

auto player = BSoundPlayer{
	name-str,
	player-callback-func,
	notifier-callback-func,
	cookie-ptr
};

player-callback-func

auto cb = [] (
	void * cookie,
	void * buffer,
	std::size_t size,
	const media_raw_audio_format & format
)
{
};

cookie

Any c++ object whose lifetime is across all callbacks.

Play

player.Start();

player.Stop();

https://www.haiku-os.org

c++ example

Note:

Total-bytes:

size__ * dimx == (dimx * dimy) * sizeof float{}

#include <SoundPlayer.h>
#include <iostream>
#include <future>
#include <bit>

namespace app
{

class cookie
{
private:
	float __incrementer = 0.0f;
public:
	virtual ~cookie()
	{
		std::cout << "cookie is removed" << std::endl;
	}
	cookie() = default;
public:
	float get()
	{
		__incrementer += 0.02f;
		if (__incrementer >= 1.0f)
			__incrementer = -1.0;
		return __incrementer;
	}
	void reset()
	{
		__incrementer = 0.0f;
	}
};

}

int main()
{
	auto cookie = app::cookie{};
	auto player = BSoundPlayer{
		"player",
		[] (
			void * cookie__,
			void * buffer__,
			std::size_t size__,
			const media_raw_audio_format & format__
		)
		{
			if (format__.format != media_raw_audio_format::B_AUDIO_FLOAT)
			{
				std::cout << "Only float format is supported by this program" << std::endl;
				return;
			}
			auto dimy = size__ / sizeof float{};
			auto dimx = format__.channel_count;

			auto buffer = std::bit_cast<float *>(buffer__);

			auto cookie = std::bit_cast<app::cookie *>(cookie__);
			cookie->reset();

			for (int j=0; j<dimy; ++j)
			{
				for (int i=0; i<dimx; ++i)
				{
					buffer[j*dimx + i] = cookie->get();
				}
			}
		},
		{},
		&cookie
	};
	player.Start();
	player.SetHasData(true);
	std::this_thread::sleep_for(std::chrono::minutes(1));
	player.Stop();
}

Build:

g++ play.cpp -std=c++23 -lmedia -o play









Jan 02, 2025

Back

Up

cpp/c++

c++ std::exception:

std::cout.write(err.data(), err.size());

std::cout << std::endl;

caught:

  ===================================
  #  The c++ programming language.  #
  #                                 #
  #  Join c++ Discord: yZcauUAUyC   #
  #  Deck                           #
  ===================================

Home: cppfx.xyz

K


PrevUpHomeNext