Comments (9)
The phone connected the PC with a 3mm audio cable, and the service running on the PC received the PCM data from the usb handle, then you can transform the PCM data to web format.
Below is my demo code:
package main
import (
"fmt"
// "io/ioutil"
"encoding/binary"
"os"
"time"
"github.com/google/gousb"
"github.com/youpy/go-wav"
"gopkg.in/hraban/opus.v2"
)
var (
CaptureNum = 500
SampleRate = 48000
ChannelNum = 1
SampleBits = 16
OpusFrameSize = 480
)
func writeUSBDataToSamples(usbData []byte) []wav.Sample {
samples := make([]wav.Sample, 0)
for i := 0; i < len(usbData); i += 2 {
sample := wav.Sample{}
sample.Values[0] = int(usbData[i])
sample.Values[0] = int(usbData[i+1])
samples = append(samples, sample)
}
return samples
}
func convertBytesToInt16(inputData []byte) []int16 {
outputData := make([]int16, 0)
inputDataLength := len(inputData)
for index := 0; index < inputDataLength-1; index += 2 {
outputData = append(outputData, int16(binary.LittleEndian.Uint16(inputData[index:index+2])))
}
return outputData
}
func main() {
var readBuffer = make([]byte, 4096)
vid, pid := gousb.ID(0x0d8c), gousb.ID(0x0014)
context := gousb.NewContext()
defer context.Close()
device, err := context.OpenDeviceWithVIDPID(vid, pid)
if err != nil {
fmt.Println(err)
}
device.SetAutoDetach(true)
configNum, _ := device.ActiveConfigNum()
config, _ := device.Config(configNum)
intf, err := config.Interface(2, 1)
if err != nil {
fmt.Println("config.Interface err: %s", err.Error())
}
endPoint, err := intf.InEndpoint(2)
if err != nil {
fmt.Println(err)
}
pcmBytesBuffer := make([]byte, 0)
beginTime := time.Now()
for i := 0; i <= CaptureNum; i++ {
readLen, err := endPoint.Read(readBuffer)
if err != nil {
fmt.Println("Read readBuffer err: %s", err.Error())
} else {
// fmt.Println("readLen ", readLen)
pcmBytesBuffer = append(pcmBytesBuffer, readBuffer[:readLen]...)
if err != nil {
fmt.Println("Write err: %s", err.Error())
}
}
// time.Sleep(time.Millisecond * 10)
}
duration := time.Now().Sub(beginTime).Seconds()
fmt.Println("pcmBytesBuffer len: ", len(pcmBytesBuffer))
fmt.Println("duration: ", duration)
fwav, _ := os.OpenFile("output.wav", os.O_RDWR|os.O_CREATE, 0777)
writer := wav.NewWriter(fwav, uint32(len(pcmBytesBuffer)), uint16(ChannelNum), uint32(SampleRate), uint16(SampleBits))
writer.Write(pcmBytesBuffer)
fwav.Close()
fpcm, _ := os.OpenFile("output.pcm", os.O_RDWR|os.O_CREATE, 0777)
fpcm.Write(pcmBytesBuffer)
fpcm.Close()
pcmInt16Data := convertBytesToInt16(pcmBytesBuffer)
enc, err := opus.NewEncoder(SampleRate, ChannelNum, opus.AppVoIP)
if err != nil {
fmt.Println("opus.NewEncoder err: ", err.Error())
}
fmt.Println("pcmInt16Data length ", len(pcmInt16Data))
opusOutput := make([]byte, 0)
opusData := make([]byte, 4096)
encodedNumSum := 0
for encodedNumSum+OpusFrameSize < len(pcmInt16Data) {
encodedNum, err := enc.Encode(pcmInt16Data[encodedNumSum:(encodedNumSum+OpusFrameSize)], opusData)
if err != nil {
fmt.Println("enc.Encode err ", err.Error())
break
}
fmt.Println("encodedNum ", encodedNum)
opusOutput = append(opusOutput, opusData[:encodedNum]...)
encodedNumSum += OpusFrameSize
}
fmt.Println("opusOutput length ", len(opusOutput))
fopus, _ := os.OpenFile("output.opus", os.O_RDWR|os.O_CREATE, 0777)
fopus.Write(opusOutput)
fopus.Close()
}
from stf.
We have a prototype for that but it requires an extra cable in the
headphone jack (or root), since it's not possible without system
permissions. (Although I'd certainly like to be proven wrong)
On Thursday, July 23, 2015, Viacheslav Frolov [email protected]
wrote:
Really great software! Thanks very much! [image: 👍]
There is a bit crazy idea - if it is ever possible - to redirect sound
input/output to browser?
Might be useful for testing.What do you think?
Also then one can finally leave phone at home and make calls from browser
:-)—
Reply to this email directly or view it on GitHub
#22.
from stf.
Hello, is there any update for this issue?
Actually I need to check the sound condition for music streaming.
from stf.
It would take so much time that I doubt we'll be able to offer the feature for free.
from stf.
Hi, do you have an update on this feature since last then? I am more than happy to pay for this feature if it is available. Thanks
from stf.
We have a prototype for that but it requires an extra cable in the
headphone jack (or root)
Could you give me some detail information about the cable, and how it works?
from stf.
I don't think it's a good idea anymore. Many new devices come without a headphone jack.
I have no time to explain it in detail but just get an analog audio -> usb converter. Then you'll get USB audio that you can read with standard utilities. You can get started with a cheap one, but they don't usually have the most suitable specs.
from stf.
@sorccu, would you please provide some details about that analog audio --> USB converter ? would you kindly share a link to a similar device that I can acquire? Many thanks.
from stf.
@likezjuisee Do you have a GitHub project that sets out relatively complete solutions and ideas?
(有建立GitHub 项目,阐述相对完整的解决思路和方案吗?)
from stf.
Related Issues (20)
- mac M1 Expected in: flat namespace
- STF- Throwing timeout after sometime HOT 1
- dyld: lazy symbol binding failed: Symbol not found: __ZN2v87Isolate37AdjustAmountOfExternalAllocatedMemoryEx HOT 2
- 远程连接手机出现Device was kicked by automatic timeout HOT 2
- Screenshots and files will not be uploaded. HOT 9
- Don't see available devices when create new group
- Why root admin is only able to see the main default froup ? is there a way to make other admin to see default group ?
- The memory used by nodes is increasing
- stf 3.6.4 not detecting devices
- Unable to install STF due to gyp ERR! configure error
- If I use password autofill feature of the STF page, the autofill dialog is displayed when I click on the device screen area.
- None
- Add "About" section to the UI
- Unexpected error checking ProtoBuf: TypeError: Invalid Version: 23.4 HOT 1
- Mac M1 stf disconnected missing symbol called HOT 1
- The device is not detecting when stf local is run through ip address
- Problem Auth System autorization
- mac openstf and ldap auth fail (Authentication Failure for "test account"
- How to set up to login by password/SSO?
- Setup had an error PrematureEOFError: Premature end of stream, needed 1 more bytes
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stf.