如何将PCM样本从麦克风转换为本机中的字节

问题描述 投票:0回答:1

我正在进行一个本机项目,该项目从麦克风读取音频样本并将其通过websocket发送进行处理。我读取的样本具有以下格式:258,-403,-595,-740,-643,-595, -836,-1109,-1141,-1317,-1428,-1059,-804,-884,-980,-788,-451,-419,-387,-82,61,157,366,462,446,606,574,478,478,767,1038,1088,1216, 1505,1617,1248,991,1007,751,494,398,221,-50,-419,-690,-788,-948,-1059,-1027,-980,-1027,-1141,-1075,-1093,-1205 ,-996,-706,-435,-146,29,-162,-338,-178,-50,-162,-322,-499,-531,-306,-18,269,574,863,783,622,558,685,1022,1104,1136 ,1070,815,606,348,205,61,-274,-611,-852,-884,-675,-595,-611,-483,-242,-66,0,269,430,269,269,398,430,269,125,333,253,-258,-162,45,-338, -627,-371,-98,-146,0,205,109,189,285,366,430,414,382,173,205,414,382,13,-419,-353,-403,-627,-675,-722,-675,-900,-1093,-932,-836,- 772,-627,-611,-595,-451,-274,-162,-387,-242,-130,-322,-98,0,-34,237,462,526,462,526,799,895,991,1104,1200,1407,1537,1697, 1697,1423,1328,1184,1070,799,221,-194,-531,-884,-996,-1043,-1189,-117 3,-1027,-916,-868,-980,-980,-868,-722,-531,-483,-515,-611,-595,-690,-900,-868,-932, -916,-804,-980,-1012,-788,-483,-114,45,45,29,253,478,382,348,510,767,1022,1054,1136,1296,1360,1136,1022,1216,1136,911,767,478

如果我将它们写入文件,它们将照原样写入。如何在将它们转储到文件之前进行转换,以便以后可以使用Audacity播放?

javascript react-native websocket audio-recording microphone
1个回答
1
投票

最后,我找到了一种方法。

class App extends React.Component {
  constructor(){
    super();
    this.state={
      status:true
    }

  }
  componentDidMount() {

    ws = new WebSocket('ws://192.168.0.2:1234');

    ws.onopen=()=>{
      console.log('Opened')
    }

    ws.onerror=(e)=>{
      console.log(e.message)
    }

    ws.onmessag=(e)=>{
      console.log(e.data);
    }
    ws.onclose=()=>{
      console.log('closed')
    }

    //Recording.start()

  }

  startVoice(){

    Recording.init({
      bufferSize: 640,
      sampleRate: 16000,
      bitsPerChannel: 16,
      channelsPerFrame: 1,
    })

    const listener = Recording.addRecordingEventListener(data => {

      //console.log(data)
      var view = new Uint16Array(data);
      ws.send(view);

    })

    Recording.start();
    this.setState({status:false});
  }

  stopVoice(){
    Recording.stop();
    this.setState({status:true});
  }

componentWillUnmount() {
    //Recording.stop()
  }

  render() {
    return(
        <View>
       {this.state.status?<Button title='start' onPress={()=>this.startVoice()}/>:<Button title='stop' onPress={()=>this.stopVoice()}/>}
       </View>
    )
  }
}

export default App;
© www.soinside.com 2019 - 2024. All rights reserved.