fork download
  1. # your code goes here
  2. #!/usr/bin/python
  3.  
  4. # http://www.remotecentral.com/features/irdisp2.htm
  5.  
  6. import numpy as N
  7. import wave
  8. import sys
  9.  
  10. class IrSoundFile:
  11. def __init__( self, make, model, signal ):
  12. self.shift = 0
  13. dir = make + '/' + model
  14. #`mkdir -p $dir`
  15. self.file = wave.open( dir + '/' + signal + '.wav', 'wb' )
  16. self.data = []
  17.  
  18. def write_data(self):
  19. data_str = ''.join(self.data)
  20. self.file.setparams( ( 2, 2, self.base_frequency, len( data_str ), 'NONE', 'noncompressed' ) )
  21. self.file.writeframes(data_str)
  22.  
  23. def close(self):
  24. self.file.close()
  25.  
  26. def add_vals(self, count, val):
  27. counter = 0
  28. half_base = self.base_frequency / 2
  29. while counter < count:
  30. if val == 1:
  31. ir_val = 0xFF7F
  32. if self.shift > ( half_base ):
  33. ir_val = 0xFFFF - ir_val
  34. else:
  35. ir_val = 0x7FFF
  36. #print( "IR_VAL: %d" % ( ir_val ) )
  37. self.data.append( wave.struct.pack( '>H', ir_val ) ) # left channel
  38. self.data.append( wave.struct.pack( '>H', ir_val ) ) # right channel
  39. old_shift = self.shift
  40. self.shift += self.frequency
  41. if self.shift > self.base_frequency: # I guess this is faster than %
  42. self.shift = self.shift - self.base_frequency
  43. if not ( ( old_shift < half_base ) and ( self.shift < half_base ) or ( old_shift > half_base ) and ( self.shift > half_base ) ):
  44. counter += 1
  45.  
  46. def add_pairs( self, ones, zeros ):
  47. self.add_vals( int( ones, 16 ), 1 )
  48. self.add_vals( int( zeros, 16 ), 0 )
  49. sys.stdout.write( "%s %s | " % ( ones, zeros ) )
  50.  
  51.  
  52. def write_ir_code( self, str ):
  53. self.base_frequency = 44100 # the freq of the wav file
  54. frequencies = { '006d' : 38200 / 2 } # IR freq / 2 (b/c we're using 2 of those)
  55. codes = str.split( ' ' )
  56. ( self.frequency, sequence_1_len, sequence_2_len ) = ( frequencies[codes[1]], int( codes[2], 16 ), int( codes[3], 16 ) )
  57. print "S1L:%d S2L:%d FRQ:%d BFRQ:%d" % ( sequence_1_len, sequence_2_len, self.frequency, self.base_frequency)
  58. print "Writing sequence 1 240 times"
  59. for j in range( 1, 4 ):
  60. for i in range( 1, sequence_1_len + 1 ):
  61. self.add_pairs( codes[i * 2 + 2], codes[i * 2 + 1 + 2] )
  62. print "\nWriting sequence 2 one time"
  63. for j in range( 1, 1 ):
  64. for i in range( 1, sequence_2_len + 1 ):
  65. self.add_pairs( codes[i * 2 + 2 + sequence_1_len], codes[i * 2 + 1 + 2 + sequence_1_len] )
  66. print ""
  67. self.write_data()
  68.  
  69.  
  70. samsung_power_code = "0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"
  71.  
  72. samsung_volplus_code = "0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"
  73.  
  74. samsung_volminus_code = "0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"
  75.  
  76. file = IrSoundFile( 'samsung', 'tv', 'power' )
  77. file.write_ir_code( samsung_power_code )
  78. file.close()
  79.  
  80. file = IrSoundFile( 'samsung', 'tv', 'volplus' )
  81. file.write_ir_code( samsung_volplus_code )
  82. file.close()
  83.  
  84. file = IrSoundFile( 'samsung', 'tv', 'volminus' )
  85. file.write_ir_code( samsung_volminus_code )
  86. file.close()
  87.  
  88.  
Runtime error #stdin #stdout #stderr 0.01s 7724KB
stdin
Standard input is empty
stdout

Standard output is empty

stderr
Traceback (most recent call last):
  File "prog.py", line 6, in <module>
    import numpy as N
ImportError: No module named numpy