├── check-output1.py ├── play.py ├── README.md ├── audio_record.py ├── pcm_channels.py └── .gitignore /check-output1.py: -------------------------------------------------------------------------------- 1 | from pcm_channels import pcm_channels 2 | 3 | m=[]; 4 | a = pcm_channels('output.wav'); 5 | 6 | print(len(a[0][0])); 7 | 8 | for l in a[0][0]: 9 | if l<=l+5 and l>=l-5: 10 | m.append(1); 11 | else: 12 | m.append(0); 13 | 14 | print(a[0][0]); -------------------------------------------------------------------------------- /play.py: -------------------------------------------------------------------------------- 1 | import pyaudio 2 | import wave 3 | import sys 4 | 5 | CHUNK = 1024 6 | 7 | if len(sys.argv) < 2: 8 | print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0]) 9 | sys.exit(-1) 10 | 11 | wf = wave.open(sys.argv[1], 'rb') 12 | 13 | p = pyaudio.PyAudio() 14 | 15 | stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), 16 | channels=wf.getnchannels(), 17 | rate=wf.getframerate(), 18 | output=True) 19 | 20 | data = wf.readframes(CHUNK) 21 | 22 | while data != b'': 23 | stream.write(data) 24 | data = wf.readframes(CHUNK) 25 | 26 | stream.stop_stream() 27 | stream.close() 28 | 29 | p.terminate(); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wav-to-pcm 2 | This contains python scripts for converting wav files to pcm data for further processing. 3 | 4 | # Requirements 5 | * pyaudio 6 | * wave 7 | 8 | # Usage 9 | First record audio using 'audio_record.py' 10 | --> This will output a 'output.wav' file 11 | 12 | Now if you run 'check-output1.py' it will print out.. 13 | * Number of frames 14 | * Array containing all the frames 15 | 16 | * Rate of the Frame capture : 44100 17 | * Total Time recorded : 5 18 | ### 19 | (Both can be changed in 'audio_record.py') 20 | * Bit length of each Frame : 16 21 | 22 | If you just want to use array of pcm data, Just include pcm_channels.py in your code and call "pcm_channels" by passing the name of the audio output file. 23 | ### 24 | Function "pcm_channels" will return a tupple containing [pcm_data, sample_rate] 25 | -------------------------------------------------------------------------------- /audio_record.py: -------------------------------------------------------------------------------- 1 | 2 | import pyaudio 3 | import wave 4 | 5 | CHUNK = 1024 6 | FORMAT = pyaudio.paInt16 7 | CHANNELS = 2 8 | RATE = 44100 9 | RECORD_SECONDS = 5 10 | WAVE_OUTPUT_FILENAME = "output.wav" 11 | 12 | p = pyaudio.PyAudio() 13 | 14 | stream = p.open(format=FORMAT, 15 | channels=CHANNELS, 16 | rate=RATE, 17 | input=True, 18 | frames_per_buffer=CHUNK) 19 | 20 | print("* recording") 21 | 22 | frames = [] 23 | 24 | for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): 25 | data = stream.read(CHUNK) 26 | frames.append(data) 27 | 28 | print("* done recording") 29 | 30 | stream.stop_stream() 31 | stream.close() 32 | p.terminate() 33 | 34 | wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') 35 | wf.setnchannels(CHANNELS) 36 | wf.setsampwidth(p.get_sample_size(FORMAT)) 37 | wf.setframerate(RATE) 38 | wf.writeframes(b''.join(frames)) 39 | wf.close() -------------------------------------------------------------------------------- /pcm_channels.py: -------------------------------------------------------------------------------- 1 | import wave 2 | import struct 3 | 4 | def pcm_channels(wave_file): 5 | stream = wave.open(wave_file,"rb") 6 | 7 | num_channels = stream.getnchannels() 8 | sample_rate = stream.getframerate() 9 | sample_width = stream.getsampwidth() 10 | num_frames = stream.getnframes() 11 | 12 | raw_data = stream.readframes( num_frames ) 13 | stream.close() 14 | 15 | total_samples = num_frames * num_channels 16 | 17 | if sample_width == 1: 18 | fmt = "%iB" % total_samples 19 | elif sample_width == 2: 20 | fmt = "%ih" % total_samples 21 | else: 22 | raise ValueError("Only supports 8 and 16 bit audio formats.") 23 | 24 | integer_data = struct.unpack(fmt, raw_data) 25 | del raw_data 26 | 27 | channels = [ [] for time in range(num_channels) ] 28 | 29 | for index, value in enumerate(integer_data): 30 | bucket = index % num_channels 31 | channels[bucket].append(value) 32 | 33 | return channels, sample_rate 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | --------------------------------------------------------------------------------