/
pa.go
110 lines (97 loc) · 2.91 KB
/
pa.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"github.com/gordonklaus/portaudio"
"github.com/takuyaohashi/go-wav"
"log"
"os"
)
//numOutputChannels int, sampleRate float64, framesPerBuffer
const (
RecordSeconds int = 5
NumInputChannels int = 1
NumOutputChannels int = 0
SampleRate int = 48000
FramesPerBuffer int = 1024
)
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
run()
}
func run() {
f, err := os.Create(`input.wav`)
check(err)
defer f.Close()
err = portaudio.Initialize()
if err != nil{
fmt.Println(err)
err = portaudio.Terminate()
fmt.Println(err)
log.Fatalln("")
}
check(err)
defer portaudio.Terminate()
inputDevice, err := portaudio.DefaultInputDevice()
check(err)
outputDevice, err := portaudio.DefaultOutputDevice()
check(err)
streamParameters := portaudio.StreamParameters{
Input: portaudio.StreamDeviceParameters{inputDevice, 1, inputDevice.DefaultLowInputLatency},
Output: portaudio.StreamDeviceParameters{outputDevice, 1, outputDevice.DefaultLowOutputLatency},
//Output: portaudio.StreamDeviceParameters{nil, 0, outputDevice.DefaultLowOutputLatency},
SampleRate: float64(SampleRate),
FramesPerBuffer: FramesPerBuffer,
Flags: portaudio.NoFlag,
}
fmt.Println(streamParameters.Input.Device.Name)
fmt.Println(streamParameters.Input.Device.MaxInputChannels)
fmt.Println(streamParameters.Input.Device.DefaultSampleRate)
fmt.Println(streamParameters.Input.Device.HostApi)
fmt.Println(streamParameters.Input.Device.DefaultLowInputLatency)
fmt.Println(streamParameters.Input.Device.DefaultHighInputLatency)
fmt.Println(streamParameters.Output.Device.Name)
fmt.Println(streamParameters.Output.Device.MaxOutputChannels)
fmt.Println(streamParameters.Output.Device.DefaultSampleRate)
fmt.Println(streamParameters.Output.Device.HostApi)
fmt.Println(streamParameters.Output.Device.DefaultLowInputLatency)
fmt.Println(streamParameters.Output.Device.DefaultHighInputLatency)
//bufferIn := make([]float32, FramesPerBuffer)
//bufferOut := make([]float32, FramesPerBuffer)
bufferIn := make([]int16, FramesPerBuffer)
bufferOut := make([]int16, FramesPerBuffer)
//open stream
stream, err := portaudio.OpenStream(streamParameters, bufferIn, bufferOut)
check(err)
defer stream.Close()
fmt.Println("info: ", stream.Info())
//make input.wav
p := wav.WriterParam{
SampleRate: 48000,
BitsPerSample: 16,
NumChannels: 1,
AudioFormat: 1,
}
w, err := wav.NewWriter(f, p)
defer w.Close()
fmt.Println("recording...")
err = stream.Start()
check(err)
//iter := int(float64(5) / (float64(FramesPerBuffer) / float64(SampleRate)))
iter := 235
for i := 0; i < iter; i++ {
err = stream.Read()
check(err)
err = stream.Write()
check(err)
//is := Float32sToInt16s(bufferIn)
w.WriteSamples(bufferIn)
}
//fmt.Println(bufferIn)
fmt.Println("end!!")
//err = stream.Write()
//if err != nil {
// fmt.Println(err)
//}
err = stream.Stop()
check(err)
}