Added support for audio recording.

Added unhandled event handler.
This commit is contained in:
Sander Schobers 2019-10-12 09:01:46 +02:00
parent 06a38d8e4a
commit 4ae1db7969
6 changed files with 88 additions and 12 deletions

37
allg5/audio.go Normal file
View File

@ -0,0 +1,37 @@
package allg5
/*
#define ALLEGRO_KCM_AUDIO_SRC
#include <allegro5/allegro_audio.h>
*/
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)
}

View File

@ -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"

View File

@ -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"

View File

@ -1,7 +1,10 @@
package allg5
/*
#define ALLEGRO_KCM_AUDIO_SRC
#include <allegro5/allegro.h>
#include <allegro5/allegro_audio.h>
#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:

View File

@ -1,13 +1,19 @@
package allg5
// #include <allegro5/allegro.h>
// #include <allegro5/allegro_font.h>
// #include <allegro5/allegro_image.h>
// #include <allegro5/allegro_primitives.h>
// #include <allegro5/allegro_ttf.h>
// bool init() {
// return al_init();
// }
/*
#define ALLEGRO_KCM_AUDIO_SRC
#include <allegro5/allegro.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_ttf.h>
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")
}

View File

@ -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)
}