Skip to content
Open
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
7 changes: 5 additions & 2 deletions audio/audio_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package audio

import (
"runtime"
"unsafe"

"github.com/Microsoft/cognitive-services-speech-sdk-go/common"
Expand All @@ -18,7 +19,7 @@ import "C"
// AudioConfig represents specific audio configuration, such as microphone, file, or custom audio streams.
type AudioConfig struct {
handle C.SPXHANDLE
properties common.PropertyCollection
properties *common.PropertyCollection
}

// GetHandle gets the handle to the resource (for internal use)
Expand All @@ -27,7 +28,8 @@ func (config AudioConfig) GetHandle() common.SPXHandle {
}

// Close releases the underlying resources
func (config AudioConfig) Close() {
func (config *AudioConfig) Close() {
runtime.SetFinalizer(config, nil)
config.properties.Close()
C.audio_config_release(config.handle)
}
Expand All @@ -41,6 +43,7 @@ func newAudioConfigFromHandle(handle C.SPXHANDLE) (*AudioConfig, error) {
config := new(AudioConfig)
config.handle = handle
config.properties = common.NewPropertyCollectionFromHandle(handle2uintptr(propBagHandle))
runtime.SetFinalizer(config, (*AudioConfig).Close)
return config, nil
}

Expand Down
6 changes: 5 additions & 1 deletion audio/audio_input_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package audio

import (
"runtime"
"sync"
"unsafe"

Expand Down Expand Up @@ -36,7 +37,8 @@ func (stream audioInputStreamBase) getHandle() C.SPXHANDLE {
return stream.handle
}

func (stream audioInputStreamBase) Close() {
func (stream *audioInputStreamBase) Close() {
runtime.SetFinalizer(stream, nil)
C.audio_stream_release(stream.handle)
}

Expand All @@ -56,6 +58,7 @@ func CreatePushAudioInputStreamFromFormat(format *AudioStreamFormat) (*PushAudio
}
stream := new(PushAudioInputStream)
stream.handle = handle
runtime.SetFinalizer(stream, (*PushAudioInputStream).Close)
return stream, nil
}

Expand Down Expand Up @@ -211,6 +214,7 @@ func CreatePullStreamFromFormat(callback PullAudioInputStreamCallback, format *A
registerCallback(handle, callback)
stream := new(PullAudioInputStream)
stream.handle = handle
runtime.SetFinalizer(stream, (*PullAudioInputStream).Close)
return stream, nil
}

Expand Down
5 changes: 5 additions & 0 deletions audio/audio_output_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package audio

import (
"runtime"
"unsafe"

"github.com/Microsoft/cognitive-services-speech-sdk-go/common"
Expand Down Expand Up @@ -36,6 +37,7 @@ func (stream *audioOutputStreamBase) getHandle() C.SPXHANDLE {
}

func (stream *audioOutputStreamBase) Close() {
runtime.SetFinalizer(stream, nil)
C.audio_stream_release(stream.handle)
}

Expand All @@ -48,6 +50,7 @@ type PullAudioOutputStream struct {
func NewPullAudioOutputStreamFromHandle(handle common.SPXHandle) *PullAudioOutputStream {
stream := new(PullAudioOutputStream)
stream.handle = uintptr2handle(handle)
runtime.SetFinalizer(stream, (*PullAudioOutputStream).Close)
return stream
}

Expand Down Expand Up @@ -110,6 +113,7 @@ func deregisterPushStreamCallback(handle C.SPXHANDLE) {
mu.Lock()
defer mu.Unlock()
pushStreamCallbacks[handle] = nil
delete(pushStreamCallbacks, handle)
}

//export cgoAudioOutputCallWriteCallback
Expand Down Expand Up @@ -149,5 +153,6 @@ func CreatePushAudioOutputStream(callback PushAudioOutputStreamCallback) (*PushA
registerPushStreamCallback(handle, callback)
stream := new(PushAudioOutputStream)
stream.handle = handle
runtime.SetFinalizer(stream, (*PushAudioOutputStream).Close)
return stream, nil
}
26 changes: 14 additions & 12 deletions audio/audio_stream_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package audio

import (
"runtime"

"github.com/Microsoft/cognitive-services-speech-sdk-go/common"
)

Expand All @@ -19,6 +21,13 @@ type AudioStreamFormat struct {
handle C.SPXHANDLE
}

func newAudioStreamFormatFromHandle(handle C.SPXHANDLE) (*AudioStreamFormat, error) {
format := new(AudioStreamFormat)
format.handle = handle
runtime.SetFinalizer(format, (*AudioStreamFormat).Close)
return format, nil
}

// GetDefaultInputFormat creates an audio stream format object representing the default audio stream format
// (16 kHz, 16 bit, mono PCM).
func GetDefaultInputFormat() (*AudioStreamFormat, error) {
Expand All @@ -27,9 +36,7 @@ func GetDefaultInputFormat() (*AudioStreamFormat, error) {
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
format := new(AudioStreamFormat)
format.handle = handle
return format, nil
return newAudioStreamFormatFromHandle(handle)
}

// GetWaveFormatPCM creates an audio stream format object with the specified PCM waveformat characteristics.
Expand All @@ -45,9 +52,7 @@ func GetWaveFormatPCM(samplesPerSecond uint32, bitsPerSample uint8, channels uin
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
format := new(AudioStreamFormat)
format.handle = handle
return format, nil
return newAudioStreamFormatFromHandle(handle)
}

// GetDefaultOutputFormat creates an audio stream format object representing the default audio stream format
Expand All @@ -58,9 +63,7 @@ func GetDefaultOutputFormat() (*AudioStreamFormat, error) {
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
format := new(AudioStreamFormat)
format.handle = handle
return format, nil
return newAudioStreamFormatFromHandle(handle)
}

// GetCompressedFormat creates an audio stream format object with the specified compressed audio container format, to be
Expand All @@ -71,12 +74,11 @@ func GetCompressedFormat(compressedFormat AudioStreamContainerFormat) (*AudioStr
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
format := new(AudioStreamFormat)
format.handle = handle
return format, nil
return newAudioStreamFormatFromHandle(handle)
}

// Close disposes the associated resources.
func (format *AudioStreamFormat) Close() {
runtime.SetFinalizer(format, nil)
C.audio_stream_format_release(format.handle)
}
13 changes: 9 additions & 4 deletions common/property_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ package common
// #include <speechapi_c_common.h>
// #include <speechapi_c_property_bag.h>
import "C"
import "unsafe"
import (
"runtime"
"unsafe"
)

// PropertyCollection is a class to retrieve or set a property value from a property collection.
type PropertyCollection struct {
Expand Down Expand Up @@ -63,13 +66,15 @@ func (properties PropertyCollection) SetPropertyByString(name string, value stri
}

// Close disposes the associated resources.
func (properties PropertyCollection) Close() {
func (properties *PropertyCollection) Close() {
runtime.SetFinalizer(properties, nil)
C.property_bag_release(properties.handle)
}

// NewPropertyCollectionFromHandle creates a PropertyCollection from a handle (for internal use)
func NewPropertyCollectionFromHandle(handle SPXHandle) PropertyCollection {
func NewPropertyCollectionFromHandle(handle SPXHandle) *PropertyCollection {
propertyCollection := new(PropertyCollection)
propertyCollection.handle = uintptr2handle(handle)
return *propertyCollection
runtime.SetFinalizer(propertyCollection, (*PropertyCollection).Close)
return propertyCollection
}
8 changes: 6 additions & 2 deletions dialog/activity_received_event_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package dialog

import (
"runtime"
"unsafe"

"github.com/Microsoft/cognitive-services-speech-sdk-go/audio"
"github.com/Microsoft/cognitive-services-speech-sdk-go/common"
)
Expand All @@ -12,15 +15,15 @@ import (
// #include <stdint.h>
// #include <speechapi_c_dialog_service_connector.h>
import "C"
import "unsafe"

type ActivityReceivedEventArgs struct {
handle C.SPXHANDLE
Activity string
}

// Close releases the underlying resources
func (event ActivityReceivedEventArgs) Close() {
func (event *ActivityReceivedEventArgs) Close() {
runtime.SetFinalizer(event, nil)
C.dialog_service_connector_activity_received_event_release(event.handle)
}

Expand All @@ -43,6 +46,7 @@ func (event ActivityReceivedEventArgs) GetAudio() (*audio.PullAudioOutputStream,
func NewActivityReceivedEventArgsFromHandle(handle common.SPXHandle) (*ActivityReceivedEventArgs, error) {
event := new(ActivityReceivedEventArgs)
event.handle = uintptr2handle(handle)
runtime.SetFinalizer(event, (*ActivityReceivedEventArgs).Close)
var size C.size_t
ret := uintptr(C.dialog_service_connector_activity_received_event_get_activity_size(event.handle, &size))
if ret != C.SPX_NOERROR {
Expand Down
48 changes: 24 additions & 24 deletions dialog/dialog_service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package dialog

import (
"runtime"
"unsafe"

"github.com/Microsoft/cognitive-services-speech-sdk-go/common"
Expand Down Expand Up @@ -90,6 +91,7 @@ func (config *dialogServiceConfigBase) Language() string {

// Close disposes the associated resources.
func (config *dialogServiceConfigBase) Close() {
runtime.SetFinalizer(config, nil)
config.config.Close()
}

Expand All @@ -102,6 +104,14 @@ type BotFrameworkConfig struct {
dialogServiceConfigBase
}

func newBotFrameworkConfigFromSpeechConfigAndHandle(speechConfig speech.SpeechConfig, handle C.SPXHANDLE) *BotFrameworkConfig {
config := new(BotFrameworkConfig)
config.config = speechConfig
config.handle = handle
runtime.SetFinalizer(config, (*BotFrameworkConfig).Close)
return config
}

// NewBotFrameworkConfigFromSubscription creates a bot framework service config instance with the specified subscription
// key and region.
func NewBotFrameworkConfigFromSubscription(subscriptionKey string, region string) (*BotFrameworkConfig, error) {
Expand All @@ -119,10 +129,7 @@ func NewBotFrameworkConfigFromSubscription(subscriptionKey string, region string
if err != nil {
return nil, err
}
config := new(BotFrameworkConfig)
config.config = *speechConfig
config.handle = handle
return config, nil
return newBotFrameworkConfigFromSpeechConfigAndHandle(*speechConfig, handle), nil
}

// NewBotFrameworkConfigFromSubscriptionAndBotID creates a bot framework service config instance with the specified subscription
Expand All @@ -144,10 +151,7 @@ func NewBotFrameworkConfigFromSubscriptionAndBotID(subscriptionKey string, regio
if err != nil {
return nil, err
}
config := new(BotFrameworkConfig)
config.config = *speechConfig
config.handle = handle
return config, nil
return newBotFrameworkConfigFromSpeechConfigAndHandle(*speechConfig, handle), nil
}

// NewBotFrameworkConfigFromAuthorizationToken creates a bot framework service config instance with the specified authorization
Expand All @@ -172,10 +176,7 @@ func NewBotFrameworkConfigFromAuthorizationToken(authorizationToken string, regi
if err != nil {
return nil, err
}
config := new(BotFrameworkConfig)
config.config = *speechConfig
config.handle = handle
return config, nil
return newBotFrameworkConfigFromSpeechConfigAndHandle(*speechConfig, handle), nil
}

// NewBotFrameworkConfigFromAuthorizationTokenAndBotID creates a bot framework service config instance with the specified authorization
Expand All @@ -202,17 +203,22 @@ func NewBotFrameworkConfigFromAuthorizationTokenAndBotID(authorizationToken stri
if err != nil {
return nil, err
}
config := new(BotFrameworkConfig)
config.config = *speechConfig
config.handle = handle
return config, nil
return newBotFrameworkConfigFromSpeechConfigAndHandle(*speechConfig, handle), nil
}

// CustomCommandsConfig defines configurations for the dialog service connector object for using a CustomCommands backend.
type CustomCommandsConfig struct {
dialogServiceConfigBase
}

func newCustomCommandsConfigFromSpeechConfigAndHandle(speechConfig speech.SpeechConfig, handle C.SPXHANDLE) *CustomCommandsConfig {
config := new(CustomCommandsConfig)
config.config = speechConfig
config.handle = handle
runtime.SetFinalizer(config, (*CustomCommandsConfig).Close)
return config
}

// NewCustomCommandsConfigFromSubscription creates a Custom Commands config instance with the specified application id,
// subscription key and region.
func NewCustomCommandsConfigFromSubscription(applicationID string, subscriptionKey string, region string) (*CustomCommandsConfig, error) {
Expand All @@ -231,10 +237,7 @@ func NewCustomCommandsConfigFromSubscription(applicationID string, subscriptionK
if err != nil {
return nil, err
}
config := new(CustomCommandsConfig)
config.config = *speechConfig
config.handle = handle
return config, nil
return newCustomCommandsConfigFromSpeechConfigAndHandle(*speechConfig, handle), nil
}

// NewCustomCommandsConfigFromAuthorizationToken creates a Custom Commands config instance with the specified application id
Expand All @@ -261,10 +264,7 @@ func NewCustomCommandsConfigFromAuthorizationToken(applicationID string, authori
if err != nil {
return nil, err
}
config := new(CustomCommandsConfig)
config.config = *speechConfig
config.handle = handle
return config, nil
return newCustomCommandsConfigFromSpeechConfigAndHandle(*speechConfig, handle), nil
}

// ApplicationID is the corresponding backend application identifier.
Expand Down
7 changes: 5 additions & 2 deletions dialog/dialog_service_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package dialog

import (
"runtime"
"unsafe"

"github.com/Microsoft/cognitive-services-speech-sdk-go/audio"
Expand All @@ -27,7 +28,7 @@ import "C"

// DialogServiceConnector connects to a speech enabled dialog backend.
type DialogServiceConnector struct {
Properties common.PropertyCollection
Properties *common.PropertyCollection
handle C.SPXHANDLE
}

Expand All @@ -40,6 +41,7 @@ func newDialogServiceConnectorFromHandle(handle C.SPXHANDLE) (*DialogServiceConn
connector := new(DialogServiceConnector)
connector.handle = handle
connector.Properties = common.NewPropertyCollectionFromHandle(handle2uintptr(propBagHandle))
runtime.SetFinalizer(connector, (*DialogServiceConnector).Close)
return connector, nil
}

Expand All @@ -65,7 +67,8 @@ func NewDialogServiceConnectorFromConfig(config DialogServiceConfig, audioConfig
}

// Close performs cleanup of resources.
func (connector DialogServiceConnector) Close() {
func (connector *DialogServiceConnector) Close() {
runtime.SetFinalizer(connector, nil)
connector.Properties.Close()
C.dialog_service_connector_handle_release(connector.handle)
}
Expand Down
Loading