Advertisement
Guest User

Untitled

a guest
Mar 7th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. import sys
  2. import os
  3. import time
  4.  
  5. startTime = time.time()
  6.  
  7. NUMBER_OF_CHANNELS = 16
  8. SAMPLE_LENGTH = NUMBER_OF_CHANNELS // 8
  9. IO_BUFFER_SIZE = 1042*1024
  10.  
  11. try:
  12. if len(sys.argv) < 2:
  13. raise ValueError('Too few input arguments.')
  14.  
  15. sampleCounter = 0
  16. inputFileName = sys.argv[1]
  17. outputDirName = os.path.splitext(inputFileName)[0]
  18.  
  19. inputFileSize = os.path.getsize(inputFileName)
  20. if inputFileSize % SAMPLE_LENGTH != 0:
  21. raise ValueError('Input file has wrong length.')
  22.  
  23. try:
  24. inputFile = open(inputFileName, 'rb')
  25. outputFiles = []
  26.  
  27. if not os.path.exists(outputDirName):
  28. os.mkdir(outputDirName)
  29.  
  30. for channel in range(0, NUMBER_OF_CHANNELS):
  31. outputFiles.append(open(outputDirName + "/" + str(channel) + ".dat", 'wb', IO_BUFFER_SIZE))
  32.  
  33. while True:
  34. multiChannelSample = inputFile.read(SAMPLE_LENGTH)
  35. if multiChannelSample == b'':
  36. break
  37.  
  38. for channel in range(0, NUMBER_OF_CHANNELS):
  39. bit = (multiChannelSample[channel // 8] >> (channel % 8)) & 1
  40. outputFiles[channel].write(b'+1\n' if (bit) else b'-1\n')
  41.  
  42. sampleCounter += 1
  43.  
  44. finally:
  45. inputFile.close()
  46. for channel in range(0, NUMBER_OF_CHANNELS):
  47. outputFiles[channel].close()
  48.  
  49. print(str(sampleCounter), " samples were processed.\n")
  50. print("--- %s seconds ---" % (time.time() - startTime))
  51.  
  52. except IOError as error:
  53. print(error, "\n")
  54.  
  55. except ValueError as error:
  56. print(error, "\n")
  57.  
  58. #input("Press ENTER to quit.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement