diff --git a/allg5/audio.go b/allg5/audio.go new file mode 100644 index 0000000..d5cc8fa --- /dev/null +++ b/allg5/audio.go @@ -0,0 +1,37 @@ +package allg5 + +/* +#define ALLEGRO_KCM_AUDIO_SRC + +#include +*/ +import "C" + +type Recorder struct { + recorder *C.ALLEGRO_AUDIO_RECORDER + Frequency int + Depth int +} + +func NewRecorder(fragments, samples, frequency, depth int) *Recorder { + var depthC C.ALLEGRO_AUDIO_DEPTH + switch depth { + case 16: + depthC = C.ALLEGRO_AUDIO_DEPTH_INT16 + default: + depthC = C.ALLEGRO_AUDIO_DEPTH_UINT8 + } + var rec = C.al_create_audio_recorder(C.size_t(fragments), C.uint(samples), C.uint(frequency), depthC, C.ALLEGRO_CHANNEL_CONF_1) + if rec == nil { + return nil + } + return &Recorder{rec, frequency, depth} +} + +func (r *Recorder) Start() { + C.al_start_audio_recorder(r.recorder) +} + +func (r *Recorder) Destroy() { + C.al_destroy_audio_recorder(r.recorder) +} diff --git a/allg5/c.go b/allg5/c.go index 1b5f2b1..b482ed5 100644 --- a/allg5/c.go +++ b/allg5/c.go @@ -2,5 +2,5 @@ package allg5 -// #cgo pkg-config: allegro-5 allegro_font-5 allegro_image-5 allegro_primitives-5 allegro_ttf-5 +// #cgo pkg-config: allegro-5 allegro_audio-5 allegro_font-5 allegro_image-5 allegro_primitives-5 allegro_ttf-5 import "C" diff --git a/allg5/c_windows.go b/allg5/c_windows.go index 3646de5..b4d91db 100644 --- a/allg5/c_windows.go +++ b/allg5/c_windows.go @@ -2,5 +2,5 @@ package allg5 -// #cgo LDFLAGS: -lallegro -lallegro_font -lallegro_image -lallegro_primitives -lallegro_ttf +// #cgo LDFLAGS: -lallegro -lallegro_audio -lallegro_font -lallegro_image -lallegro_primitives -lallegro_ttf import "C" diff --git a/allg5/event.go b/allg5/event.go index 97378ff..5261c44 100644 --- a/allg5/event.go +++ b/allg5/event.go @@ -1,7 +1,10 @@ package allg5 /* +#define ALLEGRO_KCM_AUDIO_SRC + #include +#include #define USER_EVENT_TYPE ALLEGRO_GET_EVENT_TYPE('u', 's', 'e', 'r') @@ -133,6 +136,12 @@ type MouseMoveEvent struct { Pressure float32 } +type RecorderFragmentEvent struct { + EventBase + Buffer unsafe.Pointer + Samples int +} + type UserEvent struct { EventBase } @@ -165,6 +174,10 @@ func (eq *EventQueue) RegisterKeyboard() { eq.register(C.al_get_keyboard_event_source()) } +func (eq *EventQueue) RegisterRecorder(rec *Recorder) { + eq.register(C.al_get_audio_recorder_event_source(rec.recorder)) +} + func (eq *EventQueue) RegisterUserEvents(source *UserEventSource) { eq.register(source.source) } @@ -173,6 +186,9 @@ func (eq *EventQueue) mapEvent(e *C.ALLEGRO_EVENT) Event { any := (*C.ALLEGRO_ANY_EVENT)(unsafe.Pointer(e)) eb := EventBase{float64(any.timestamp)} switch any._type { + case C.ALLEGRO_EVENT_AUDIO_RECORDER_FRAGMENT: + recorder := (*C.ALLEGRO_AUDIO_RECORDER_EVENT)(unsafe.Pointer(e)) + return &RecorderFragmentEvent{eb, unsafe.Pointer(recorder.buffer), int(recorder.samples)} case C.ALLEGRO_EVENT_DISPLAY_CLOSE: return &DisplayCloseEvent{eb} case C.ALLEGRO_EVENT_DISPLAY_ORIENTATION: diff --git a/allg5/system.go b/allg5/system.go index aa3d1bb..478f247 100644 --- a/allg5/system.go +++ b/allg5/system.go @@ -1,13 +1,19 @@ package allg5 -// #include -// #include -// #include -// #include -// #include -// bool init() { -// return al_init(); -// } +/* +#define ALLEGRO_KCM_AUDIO_SRC + +#include +#include +#include +#include +#include +#include + +bool init() { + return al_init(); +} +*/ import "C" import ( @@ -20,6 +26,7 @@ func init() { } type InitConfig struct { + Audio bool Font bool Image bool Primitives bool @@ -27,13 +34,16 @@ type InitConfig struct { Mouse bool } -var InitAll = InitConfig{true, true, true, true, true} +var InitAll = InitConfig{true, true, true, true, true, true} // Init initializes the Allegro system func Init(config InitConfig) error { if !bool(C.init()) { return errors.New("failed to initialize Allegro") } + if config.Audio && !bool(C.al_install_audio()) { + return errors.New("failed to initialize audio addon") + } if config.Font && !bool(C.al_init_font_addon()) { return errors.New("failed to initialize font addon") } diff --git a/ui/allg5ui/renderer.go b/ui/allg5ui/renderer.go index b190ae8..b936262 100644 --- a/ui/allg5ui/renderer.go +++ b/ui/allg5ui/renderer.go @@ -34,13 +34,14 @@ func NewRenderer(w, h int, opts allg5.NewDisplayOptions) (*Renderer, error) { eq.RegisterDisplay(disp) eq.RegisterUserEvents(user) - return &Renderer{disp, eq, map[string]*font{}, user, ui.MouseCursorDefault, ui.MouseCursorDefault}, nil + return &Renderer{disp, eq, nil, map[string]*font{}, user, ui.MouseCursorDefault, ui.MouseCursorDefault}, nil } // Renderer implements ui.Renderer using Allegro 5. type Renderer struct { disp *allg5.Display eq *allg5.EventQueue + unh func(allg5.Event) ft map[string]*font user *allg5.UserEventSource cursor ui.MouseCursor @@ -77,6 +78,10 @@ func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) { t.Handle(&ui.MouseMoveEvent{MouseEvent: mouseEvent(e.MouseEvent), MouseWheel: float32(e.DeltaZ)}) case *allg5.UserEvent: t.Handle(&ui.RefreshEvent{EventBase: eventBase(e)}) + default: + if r.unh != nil { + r.unh(e) + } } ev = r.eq.Get() } @@ -98,6 +103,10 @@ func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) { } } +func (r *Renderer) RegisterRecorder(rec *allg5.Recorder) { + r.eq.RegisterRecorder(rec) +} + func (r *Renderer) Refresh() { r.user.EmitEvent() } @@ -238,6 +247,10 @@ func (r *Renderer) SetMouseCursor(c ui.MouseCursor) { r.newCursor = c } +func (r *Renderer) SetUnhandledEventHandler(handler func(allg5.Event)) { + r.unh = handler +} + func (r *Renderer) SetWindowTitle(t string) { r.disp.SetWindowTitle(t) }