track.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # greaseweazle/track.py
  2. #
  3. # Written & released by Keir Fraser <keir.xen@gmail.com>
  4. #
  5. # This is free and unencumbered software released into the public domain.
  6. # See the file COPYING for more details, or visit <http://unlicense.org>.
  7. import binascii
  8. from bitarray import bitarray
  9. from greaseweazle.flux import Flux
  10. # A pristine representation of a track, from a codec and/or a perfect image.
  11. class MasterTrack:
  12. @property
  13. def bitrate(self):
  14. return len(self.bitarray) / self.time_per_rev
  15. # bits: Track bitcell data (bitarray)
  16. # time_per_rev: Time per revolution, in seconds (float)
  17. # bit_ticks: Per-bitcell time values, in unitless 'ticks'
  18. # splice: Location of the track splice, in bitcells, after the index.
  19. def __init__(self, bits, time_per_rev):
  20. self.bits = bits
  21. self.time_per_rev = time_per_rev
  22. self.bit_ticks = None
  23. self.splice = 0
  24. def __str__(self):
  25. s = "\nMaster Track:\n"
  26. s += (" %d bits, %.1f kbit/s"
  27. % (len(self.bits), self.bitrate))
  28. if self.bit_ticks:
  29. s += " (variable)"
  30. s += ("\n Total Time = %.1f ms (%.1f rpm)\n "
  31. % (self.time_per_rev * 1000, 60 / self.time_per_rev))
  32. #s += str(binascii.hexlify(self.bits.tobytes()))
  33. return s
  34. def flux_for_writeout(self):
  35. # We're going to mess with the track data, so take a copy.
  36. bits = self.bits.copy()
  37. bitlen = len(bits)
  38. # Also copy the bit_ticks array (or create a dummy one), and remember
  39. # the total ticks that it contains.
  40. bit_ticks = self.bit_ticks.copy() if self.bit_ticks else [1] * bitlen
  41. ticks_to_index = sum(bit_ticks)
  42. splice_at_index = self.splice < 4 or bitlen - self.splice < 4
  43. if splice_at_index:
  44. # Splice is at the index (or within a few bitcells of it).
  45. # We stretch the track with extra bytes of filler, in case the
  46. # drive motor spins slower than expected and we need more filler
  47. # to get us to the index pulse (where the write will terminate).
  48. # Thus if the drive spins slow, the track gets a longer footer.
  49. pos = bitlen-4 if self.splice < 4 else self.splice-4
  50. tick_pattern = bit_ticks[pos-32:pos]
  51. fill_pattern = bits[pos-32:pos]
  52. # We stretch by 10 percent, which is way more than enough.
  53. for i in range(bitlen // (10*32)):
  54. bit_ticks[pos:pos+32] = tick_pattern
  55. bits[pos:pos+32] = fill_pattern
  56. pos += 32
  57. else:
  58. # Splice is not at the index. We will write more than one
  59. # revolution, and terminate the second revolution at the splice.
  60. # For the first revolution we repeat the track header *backwards*
  61. # to the very start of the write. This is in case the drive motor
  62. # spins slower than expected and the write ends before the original
  63. # splice position.
  64. # Thus if the drive spins slow, the track gets a longer header.
  65. bit_ticks += bit_ticks[:self.splice-4]
  66. bits += bits[:self.splice-4]
  67. pos = self.splice+4
  68. fill_pattern = bits[pos:pos+32]
  69. while pos >= 32:
  70. pos -= 32
  71. bits[pos:pos+32] = fill_pattern
  72. # Convert the stretched track data into flux.
  73. bit_ticks_i = iter(bit_ticks)
  74. flux_list = []
  75. flux_ticks = 0
  76. for bit in bits:
  77. flux_ticks += next(bit_ticks_i)
  78. if bit:
  79. flux_list.append(flux_ticks)
  80. flux_ticks = 0
  81. if flux_ticks:
  82. flux_list.append(flux_ticks)
  83. # Package up the flux for return.
  84. flux = Flux([ticks_to_index], flux_list,
  85. self.time_per_rev / ticks_to_index)
  86. flux.terminate_at_index = splice_at_index
  87. return flux
  88. # Local variables:
  89. # python-indent: 4
  90. # End: