Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import os
- import time
- startTime = time.time()
- NUMBER_OF_CHANNELS = 16
- SAMPLE_LENGTH = NUMBER_OF_CHANNELS // 8
- IO_BUFFER_SIZE = 1042*1024
- try:
- if len(sys.argv) < 2:
- raise ValueError('Too few input arguments.')
- sampleCounter = 0
- inputFileName = sys.argv[1]
- outputDirName = os.path.splitext(inputFileName)[0]
- inputFileSize = os.path.getsize(inputFileName)
- if inputFileSize % SAMPLE_LENGTH != 0:
- raise ValueError('Input file has wrong length.')
- try:
- inputFile = open(inputFileName, 'rb')
- outputFiles = []
- if not os.path.exists(outputDirName):
- os.mkdir(outputDirName)
- for channel in range(0, NUMBER_OF_CHANNELS):
- outputFiles.append(open(outputDirName + "/" + str(channel) + ".dat", 'wb', IO_BUFFER_SIZE))
- while True:
- multiChannelSample = inputFile.read(SAMPLE_LENGTH)
- if multiChannelSample == b'':
- break
- for channel in range(0, NUMBER_OF_CHANNELS):
- bit = (multiChannelSample[channel // 8] >> (channel % 8)) & 1
- outputFiles[channel].write(b'+1\n' if (bit) else b'-1\n')
- sampleCounter += 1
- finally:
- inputFile.close()
- for channel in range(0, NUMBER_OF_CHANNELS):
- outputFiles[channel].close()
- print(str(sampleCounter), " samples were processed.\n")
- print("--- %s seconds ---" % (time.time() - startTime))
- except IOError as error:
- print(error, "\n")
- except ValueError as error:
- print(error, "\n")
- #input("Press ENTER to quit.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement