Giter Site home page Giter Site logo

Comments (9)

likezjuisee avatar likezjuisee commented on September 13, 2024 1

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.

sorccu avatar sorccu commented on September 13, 2024

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.

hong3e avatar hong3e commented on September 13, 2024

Hello, is there any update for this issue?
Actually I need to check the sound condition for music streaming.

from stf.

sorccu avatar sorccu commented on September 13, 2024

It would take so much time that I doubt we'll be able to offer the feature for free.

from stf.

alisamir82 avatar alisamir82 commented on September 13, 2024

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.

likezjuisee avatar likezjuisee commented on September 13, 2024

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.

sorccu avatar sorccu commented on September 13, 2024

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.

alisamir82 avatar alisamir82 commented on September 13, 2024

@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.

pinotao avatar pinotao commented on September 13, 2024

@likezjuisee Do you have a GitHub project that sets out relatively complete solutions and ideas?
(有建立GitHub 项目,阐述相对完整的解决思路和方案吗?)

from stf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.