/
audio_stream_format.go
82 lines (73 loc) · 2.85 KB
/
audio_stream_format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
package audio
import (
"github.com/Microsoft/cognitive-services-speech-sdk-go/common"
)
// #include <stdlib.h>
// #include <stdint.h>
// #include <speechapi_c_common.h>
// #include <speechapi_c_audio_stream_format.h>
import "C"
// AudioStreamFormat represents the audio stream format used for custom audio input configurations.
// Updated in version 1.5.0.
type AudioStreamFormat struct {
handle C.SPXHANDLE
}
// GetDefaultInputFormat creates an audio stream format object representing the default audio stream format
// (16 kHz, 16 bit, mono PCM).
func GetDefaultInputFormat() (*AudioStreamFormat, error) {
var handle C.SPXHANDLE
ret := uintptr(C.audio_stream_format_create_from_default_input(&handle))
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
format := new(AudioStreamFormat)
format.handle = handle
return format, nil
}
// GetWaveFormatPCM creates an audio stream format object with the specified PCM waveformat characteristics.
// Note: Currently, only WAV / PCM with 16-bit samples, 16 kHz sample rate, and a single channel (Mono) is supported. When
// used with Conversation Transcription, eight channels are supported.
func GetWaveFormatPCM(samplesPerSecond uint32, bitsPerSample uint8, channels uint8) (*AudioStreamFormat, error) {
var handle C.SPXHANDLE
ret := uintptr(C.audio_stream_format_create_from_waveformat_pcm(
&handle,
(C.uint32_t)(samplesPerSecond),
(C.uint8_t)(bitsPerSample),
(C.uint8_t)(channels)))
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
format := new(AudioStreamFormat)
format.handle = handle
return format, nil
}
// GetDefaultOutputFormat creates an audio stream format object representing the default audio stream format
// (16 kHz, 16 bit, mono PCM).
func GetDefaultOutputFormat() (*AudioStreamFormat, error) {
var handle C.SPXHANDLE
ret := uintptr(C.audio_stream_format_create_from_default_output(&handle))
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
format := new(AudioStreamFormat)
format.handle = handle
return format, nil
}
// GetCompressedFormat creates an audio stream format object with the specified compressed audio container format, to be
// used as input format.
func GetCompressedFormat(compressedFormat AudioStreamContainerFormat) (*AudioStreamFormat, error) {
var handle C.SPXHANDLE
ret := uintptr(C.audio_stream_format_create_from_compressed_format(&handle, (C.Audio_Stream_Container_Format)(compressedFormat)))
if ret != C.SPX_NOERROR {
return nil, common.NewCarbonError(ret)
}
format := new(AudioStreamFormat)
format.handle = handle
return format, nil
}
// Close disposes the associated resources.
func (format *AudioStreamFormat) Close() {
C.audio_stream_format_release(format.handle)
}