Python open raw audio data file

前端 未结 3 1278
抹茶落季
抹茶落季 2020-12-18 03:19

I have these files with the extension \".adc\". They are simply raw data files. I can open them with Audacity using File->Import->Raw data with encoding \"Signed 16 bit\" an

相关标签:
3条回答
  • 2020-12-18 03:32

    Thanx a lot I was able to do the following:

    def play_data(filename, first_sec, second_sec):
      import ao
      from ao import AudioDevice 
      dev = AudioDevice(2, bits=16, rate=16000,channels=1)
      f = open(filename, 'r')
      data_len = (second_sec-first_sec)*32000
      f.seek(32000*first_sec)
      data = f.read(data_len)
      dev.play(data)
      f.close()
    
    play_data('AR001_3.adc', 2.5, 5)
    
    0 讨论(0)
  • 2020-12-18 03:35

    You can use PySoundFile to open the file as a NumPy array and play it with python-sounddevice.

    import soundfile as sf
    import sounddevice as sd
    
    sig, fs = sf.read('myfile.adc', channels=2, samplerate=16000,
                      format='RAW', subtype='PCM_16')
    sd.play(sig, fs)
    

    You can use indexing on the NumPy array to select a certain part of the audio data.

    0 讨论(0)
  • 2020-12-18 03:52

    For opening the file, you just need file(). For finding a location, you don't need audioop: you just need to convert seconds to bytes and get the required bytes of the file. For instance, if your file is 16 kHz 16bit mono, each second is 32,000 bytes of data. So the 10th second is 320kB into the file. Just seek to the appropriate place in the file and then read the appropriate number of bytes.

    And audioop can't help you with the hardest part: namely, playing the audio. The correct way to do this very much depends on your OS.

    EDIT: Sorry, I just noticed your username is "thelinuxer". Consider pyAO for playing audio from Python on Linux. You will probably need to change the sample format to play the audio---audioop will help you with this (see ratecv, tomono/tostereo, lin2lin, and bias)

    0 讨论(0)
提交回复
热议问题