Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oto 1.0 driver for the Kinc library #200

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions driver_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !js
// +build !js
//go:build !js && !kinc
// +build !js && !kinc

package oto

Expand Down
119 changes: 119 additions & 0 deletions driver_kinc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2022 Sam Hocevar <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the license.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build kinc
// +build kinc

package oto

/*
#include <kinc/audio2/audio.h>

typedef void (*audio_callback_t)(kinc_a2_buffer_t *buffer, int samples);
typedef void (*sample_rate_callback_t)(void);

void audio_callback(kinc_a2_buffer_t *buffer, int samples);
void sample_rate_callback(void);
*/
import "C"

import (
"sync"
"unsafe"
)

const (
MaxBufferLength = 4096 // Max number of samples we want in the audio queue
)

type driver struct {
buf []int16
mutex sync.Mutex
}

var gDriver *driver

func (buffer *C.kinc_a2_buffer_t) SampleCount() int {
return int((buffer.write_location + buffer.data_size - buffer.read_location) % buffer.data_size) / 4
}

//export audio_callback
func audio_callback(buffer *C.kinc_a2_buffer_t, samples int32) {
gDriver.mutex.Lock()
defer gDriver.mutex.Unlock()

// Protect against accessing gDriver.buf[0]
if len(gDriver.buf) == 0 {
return
}

// Bluntly drop data if our buffer is too full
if buffer.SampleCount() >= MaxBufferLength {
gDriver.buf = gDriver.buf[:]
return
}

dst := unsafe.Slice((*float32)(unsafe.Pointer(buffer.data)), int(buffer.data_size) / 4)
for i := 0; i < int(samples); i++ {
if i < len(gDriver.buf) {
dst[buffer.write_location / 4] = float32(gDriver.buf[i]) / 32768
} else {
dst[buffer.write_location / 4] = 0
}

buffer.write_location += 4
if buffer.write_location >= buffer.data_size {
buffer.write_location = 0
}
}
gDriver.buf = gDriver.buf[min(len(gDriver.buf), int(samples)):]
}

//export sample_rate_callback
func sample_rate_callback() {
// TODO: handle rate changes?
}

func newDriver(sampleRate, numChans, bitDepthInBytes, bufferSizeInBytes int) (tryWriteCloser, error) {
p := &driver{
buf: []int16{},
}

C.kinc_a2_init()
C.kinc_a2_set_callback((C.audio_callback_t)(C.audio_callback))
gDriver = p

return p, nil
}

func (p *driver) TryWrite(data []byte) (n int, err error) {
p.mutex.Lock()
defer p.mutex.Unlock()

samples := unsafe.Slice((*int16)(unsafe.Pointer(&data[0])), len(data) / 2)
sampleCount := min(len(samples), max(0, MaxBufferLength - len(p.buf)))
p.buf = append(p.buf, samples[:sampleCount]...)
return sampleCount * 2, nil
}

func (p *driver) Close() error {
C.kinc_a2_shutdown()
gDriver = nil

return nil
}

func (d *driver) tryWriteCanReturnWithoutWaiting() bool {
return true
}
4 changes: 2 additions & 2 deletions driver_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !js && !android && !ios
// +build !js,!android,!ios
//go:build !js && !android && !ios && !kinc
// +build !js,!android,!ios,!kinc

package oto

Expand Down
4 changes: 2 additions & 2 deletions driver_macos.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build darwin && !ios && !js
// +build darwin,!ios,!js
//go:build darwin && !ios && !js && !kinc
// +build darwin,!ios,!js,!kinc

package oto

Expand Down
4 changes: 2 additions & 2 deletions driver_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !js
// +build !js
//go:build !js && !kinc
// +build !js,!kinc

package oto

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/hajimehoshi/oto

go 1.13
go 1.17

require (
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6
Expand Down
4 changes: 2 additions & 2 deletions winmm_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !js
// +build !js
//go:build !js && !kinc
// +build !js && !kinc

package oto

Expand Down