Skip to content

Fix typos and added MicRecorder implementation #1

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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: 4 additions & 3 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ func pipe(ctx context.Context, ai io.Reader, ao io.Writer, bi io.Reader, bo io.W
return
}

func execAction(method string, stdin io.Reader, stdout io.Writer, action string) error {
func execAction(method string, stdin io.Reader, stdout io.Writer, action string, args map[string]interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), GlobalTimeout)
defer cancel()
return execActionContext(ctx, stdin, stdout, method, action)
return execActionContext(ctx, stdin, stdout, method, action, args)
}

func execActionContext(ctx context.Context, stdin io.Reader, stdout io.Writer, method string, action string) error {
func execActionContext(ctx context.Context, stdin io.Reader, stdout io.Writer, method string, action string, args map[string]interface{}) error {
call := api.Call{
Method: method,
Action: action,
Args: args,
}

call.Call(ctx)
Expand Down
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/eternal-flame-AD/go-termux

go 1.17

require github.com/twinj/uuid v1.0.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/myesui/uuid v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/stretchr/testify.v1 v1.2.2 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/myesui/uuid v1.0.0 h1:xCBmH4l5KuvLYc5L7AS7SZg9/jKdIFubM7OVoLqaQUI=
github.com/myesui/uuid v1.0.0/go.mod h1:2CDfNgU0LR8mIdO8vdWd8i9gWWxLlcoIGGpSNgafq84=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/twinj/uuid v1.0.0 h1:fzz7COZnDrXGTAOHGuUGYd6sG+JMq+AoE7+Jlu0przk=
github.com/twinj/uuid v1.0.0/go.mod h1:mMgcE1RHFUFqe5AfiwlINXisXfDGro23fWdPUfOMjRY=
gopkg.in/stretchr/testify.v1 v1.2.2 h1:yhQC6Uy5CqibAIlk1wlusa/MJ3iAN49/BsR/dCCKz3M=
gopkg.in/stretchr/testify.v1 v1.2.2/go.mod h1:QI5V/q6UbPmuhtm10CaFZxED9NreB8PnFYN9JcR6TxU=
64 changes: 64 additions & 0 deletions microphone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package termux

import (
"bytes"
"encoding/json"
"os"
"path"
)

// RecordInfo represent the current state of recording process
type RecordInfo struct {
IsRecording bool `json:"isRecording"`
OutputFile string `json:"outputFile"`
}

// Record records from microphone, duration must be in seconds
func Record(duration int, outFile string) error {
buf := bytes.NewBuffer([]byte{})
if !path.IsAbs(outFile) {
wd, err := os.Getwd()
if err != nil {
return err
}
outFile = path.Join(wd, outFile)
}
if err := execAction("MicRecorder", nil, buf, "record", map[string]interface{}{
"limit": duration * 1000,
"file": outFile,
}); err != nil {
return err
}
res := buf.Bytes()
return checkErr(res)
}

// GetRecordInfo returns the current state of the recording process
func GetRecordInfo() (RecordInfo, error) {
var info RecordInfo
buf := bytes.NewBuffer([]byte{})
if err := execAction("MicRecorder", nil, buf, "info", nil); err != nil {
return RecordInfo{}, err
}
res := buf.Bytes()
if err := checkErr(res); err != nil {
return RecordInfo{}, err
}
if err := json.Unmarshal(res, &info); err != nil {
return RecordInfo{}, err
}
return info, nil
}

// StopRecord stop the recording process
func StopRecord() error {
buf := bytes.NewBuffer([]byte{})
if err := execAction("MicRecorder", nil, buf, "quit", nil); err != nil {
return err
}
res := buf.Bytes()
if err := checkErr(res); err != nil {
return err
}
return nil
}
6 changes: 3 additions & 3 deletions sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
// SensorList acquires a list of available sensors on the device
func SensorList() ([]string, error) {
buf := bytes.NewBuffer([]byte{})
if err := execAction("Sensor", nil, buf, "list"); err != nil {
if err := execAction("Sensor", nil, buf, "list", nil); err != nil {
return nil, err
}
res := buf.Bytes()

if err := checkErr(res); res != nil {
if err := checkErr(res); err != nil {
return nil, err
}
l := new(struct {
Expand Down Expand Up @@ -59,7 +59,7 @@ func Sensor(ctx context.Context, opt SensorWatchOpt) (<-chan []byte, error) {
}

go func() {
defer execAction("Sensor", nil, bytes.NewBuffer([]byte{}), "cleanup")
defer execAction("Sensor", nil, bytes.NewBuffer([]byte{}), "cleanup", nil)
for {
select {
case <-ctx.Done():
Expand Down
4 changes: 2 additions & 2 deletions wifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ type WifiAP struct {
// WifiScan scans for available networks
func WifiScan() ([]WifiAP, error) {
buf := bytes.NewBuffer([]byte{})
execAction("WifiScanInfo", nil, buf, "list")
execAction("WifiScanInfo", nil, buf, "list", nil)
res := buf.Bytes()

if err := checkErr(res); res != nil {
if err := checkErr(res); err != nil {
return nil, err
}
l := make([]WifiAP, 0)
Expand Down