McRogueFace/src/PySound.h
John McCardle c025cd7da3 feat: Add Sound/Music classes, keyboard state, version (#66, #160, #164)
Replace module-level audio functions with proper OOP API:
- mcrfpy.Sound: Wraps sf::SoundBuffer + sf::Sound for short effects
- mcrfpy.Music: Wraps sf::Music for streaming long tracks
- Both support: volume, loop, playing, duration, play/pause/stop
- Music adds position property for seeking

Add mcrfpy.keyboard singleton for real-time modifier state:
- shift, ctrl, alt, system properties (bool, read-only)
- Queries sf::Keyboard::isKeyPressed() directly

Add mcrfpy.__version__ = "1.0.0" for version identity

Remove old audio API entirely (no deprecation - unused in codebase):
- createSoundBuffer, loadMusic, playSound
- setMusicVolume, getMusicVolume, setSoundVolume, getSoundVolume

closes #66, closes #160, closes #164

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 16:24:27 -05:00

76 lines
2.3 KiB
C++

#pragma once
#include "Common.h"
#include "Python.h"
class PySound;
typedef struct {
PyObject_HEAD
std::shared_ptr<PySound> data;
} PySoundObject;
class PySound : public std::enable_shared_from_this<PySound>
{
private:
sf::SoundBuffer buffer;
sf::Sound sound;
std::string source;
bool loaded;
public:
PySound(const std::string& filename);
// Playback control
void play();
void pause();
void stop();
// Properties
float getVolume() const;
void setVolume(float vol);
bool getLoop() const;
void setLoop(bool loop);
bool isPlaying() const;
float getDuration() const;
// Python interface
PyObject* pyObject();
static PyObject* repr(PyObject* obj);
static Py_hash_t hash(PyObject* obj);
static int init(PySoundObject* self, PyObject* args, PyObject* kwds);
static PyObject* pynew(PyTypeObject* type, PyObject* args, PyObject* kwds);
// Python methods
static PyObject* py_play(PySoundObject* self, PyObject* args);
static PyObject* py_pause(PySoundObject* self, PyObject* args);
static PyObject* py_stop(PySoundObject* self, PyObject* args);
// Python getters/setters
static PyObject* get_volume(PySoundObject* self, void* closure);
static int set_volume(PySoundObject* self, PyObject* value, void* closure);
static PyObject* get_loop(PySoundObject* self, void* closure);
static int set_loop(PySoundObject* self, PyObject* value, void* closure);
static PyObject* get_playing(PySoundObject* self, void* closure);
static PyObject* get_duration(PySoundObject* self, void* closure);
static PyObject* get_source(PySoundObject* self, void* closure);
static PyMethodDef methods[];
static PyGetSetDef getsetters[];
};
namespace mcrfpydef {
static PyTypeObject PySoundType = {
.ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0},
.tp_name = "mcrfpy.Sound",
.tp_basicsize = sizeof(PySoundObject),
.tp_itemsize = 0,
.tp_repr = PySound::repr,
.tp_hash = PySound::hash,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("Sound effect object for short audio clips"),
.tp_methods = PySound::methods,
.tp_getset = PySound::getsetters,
.tp_init = (initproc)PySound::init,
.tp_new = PyType_GenericNew,
};
}