Code: Alles auswählen
def reverse_bits(b, length=8):
r = 0
for _ in range(length):
r = (r << 1) | (b & 1)
b >>= 1
return r
Code: Alles auswählen
def reverse_bits(b, length=8):
r = 0
for _ in range(length):
r = (r << 1) | (b & 1)
b >>= 1
return r
Code: Alles auswählen
BASIC_TOKENS = {
128: " FOR ", # 0x80
129: " GO ", # 0x81
130: " REM ", # 0x82
131: "'", # 0x83
132: " ELSE ", # 0x84
133: " IF ", # 0x85
...
Code: Alles auswählen
def block2ascii(bit_list):
for block in iter_steps(bit_list, steps=8):
byte_no = bits2ASCII(block)
if byte_no in BASIC_TOKENS:
character = BASIC_TOKENS[byte_no]
else:
character = chr(byte_no)
print "%s %4s %3s %s" % (
list2str(block), hex(byte_no), byte_no, repr(character)
)
Code: Alles auswählen
...
11100001 0x87 135 ' PRINT '
00000100 0x20 32 ' '
10010010 0x49 73 'I'
11011100 0x3b 59 ';'
01000100 0x22 34 '"'
00010010 0x48 72 'H'
10100010 0x45 69 'E'
00110010 0x4c 76 'L'
00110010 0x4c 76 'L'
11110010 0x4f 79 'O'
00000100 0x20 32 ' '
11101010 0x57 87 'W'
11110010 0x4f 79 'O'
01001010 0x52 82 'R'
00110010 0x4c 76 'L'
00100010 0x44 68 'D'
10000100 0x21 33 '!'
01000100 0x22 34 '"'
00000000 0x0 0 '\x00'
...
Code: Alles auswählen
TEST_STR=(
"00010010" # 0x48 72 'H'
"10100010" # 0x45 69 'E'
"00110010" # 0x4c 76 'L'
"00110010" # 0x4c 76 'L'
"11110010" # 0x4f 79 'O'
"00000100" # 0x20 32 ' '
"11101010" # 0x57 87 'W'
"11110010" # 0x4f 79 'O'
"01001010" # 0x52 82 'R'
"00110010" # 0x4c 76 'L'
"00100010" # 0x44 68 'D'
"10000100" # 0x21 33 '!'
)# 000100101010001000110010001100101111001000000100111010101111001001001010001100100010001010000100
test_start = get_start_pos_iter_window(bit_list, TEST_STR)
print "*** Test String found at:", test_start
Code: Alles auswählen
...
sentinel = c_ubyte()
i_bits = iter(bits)
for bit in i_bits:
sentinel.value <<= 1
sentinel.value |= bit
if sentinel.value == 42: # ?? nicht spec-konform
break
...[/quote]
Hab mir den code nochmal genauer angesehen.
Kann es sein, das im Grunde hier in dem Teil im Prinzip das selbe gemacht wird, wie bei mir? Also das man bit für bit durchgeht und ein bestimmtes Muster zu suchen? In diesem Fall halt [b]START_LEADER = "10101010"[/b]
Wobei ich mir nun überlegt habe, das gerade [b]10101010[/b] ziemlich doof ist. Denn die genau Position kann in diesem Fall doch sehr leicht um 2 Bits vor/zurück verschoben sein und doch passt es.
Bei http://www.onastick.clara.net/cosio.htm steht auch was von einem sync byte:
[b]1. A leader byte - $55
2. A sync byte - $3C[/b]
Kann es sein, das man besser das suchen sollte?
EDIT: Kann es sein, das 0x3C das ist: [b]00111100[/b] ?
Das würde auch mehr Sinn machen!
Mit HelloWorld1 origin.wav (was nicht funktioniert):*** file info block data:
00111100 00000000 11110000...
Hab das "sync byte" mal unterstrichen. Wie es aussieht ist das "verschoben"...*** file info block data:
10101000 11110000 0000001...
Code: Alles auswählen
def goto_next_block(bit_list, debug=False):
"""
>>> bits = (
... "10101010" # 0x55 leader byte
... "00111100" # 0x3C sync byte
... "00010010" # 0x48 'H'
... )
>>> bit_list = [int(i) for i in bits]
>>> bit_list = goto_next_block(bit_list)
>>> bit_list
[0, 0, 0, 1, 0, 0, 1, 0]
more bits inserted:
>>> bits = ("1010" # inserted
... "101010100011110000010010")
>>> goto_next_block([int(i) for i in bits])
[0, 0, 0, 1, 0, 0, 1, 0]
no complete leader byte
>>> bits = ("1010" # incomplete
... "0011110000010010")
>>> goto_next_block([int(i) for i in bits])
[0, 0, 0, 1, 0, 0, 1, 0]
"""
end = get_last_pos_iter_steps(bit_list, LEADER_BYTE)
if not end:
if debug:
print "INFO: leader byte block end not found."
else:
if debug:
print "leader byte block end found at:", end
bit_list = bit_list[end:]
sync_pos = get_start_pos_iter_window(bit_list, SYNC_BYTE)
if sync_pos is None:
if debug:
print "ERROR: Sync byte '%s' not found!" % SYNC_BYTE
sys.exit(-1)
if debug:
print "Sync byte '%s' found at position: %i" % (SYNC_BYTE, sync_pos)
# Cut until sync byte
cut_pos = sync_pos + len(SYNC_BYTE)
bit_list = bit_list[cut_pos:]
return bit_list
Na, das ist ja wohl schnell rauszufinden. Wenn man keine Lust hat, selber zu rechnen:jens hat geschrieben:EDIT: Kann es sein, das 0x3C das ist: 00111100 ?
Das würde auch mehr Sinn machen!
Code: Alles auswählen
>>> bin(0x3c)
'0b111100'
Wie kann man eigentlich das auf "einfache" Weise auffüllen? Also das man wieder 00111100 erhält?snafu hat geschrieben:Code: Alles auswählen
>>> bin(0x3c) '0b111100'
Code: Alles auswählen
"{:08b}".format(0x3c)
Code: Alles auswählen
In [7]: '{0:08b}'.format(0x3c)
Out[7]: '00111100'
Code: Alles auswählen
def iter_tokenized_lines(bytestream):
while True:
next_line_pointer = bytestream.get_word()
if next_line_pointer == 0:
break
line_number = bytestream.get_word()
tokenized_line = bytestream.get_until('\x00')
yield (line_number, tokenized_line)
Python hat auch Hex-Literale, die kann man auch bei Dicts als Schlüssel nutzen, dann spart man sich die seltsame Umrechnung und die Kommentare.jens hat geschrieben:Hab gerade nochmal einen pull des aktuellen Stands gemacht: https://github.com/jedie/python-code-sn ... 9b258cea6d
Hab ein BASIC token dict eingefügt, sieht so aus:Code: Alles auswählen
BASIC_TOKENS = { 128: " FOR ", # 0x80 129: " GO ", # 0x81 130: " REM ", # 0x82 131: "'", # 0x83 132: " ELSE ", # 0x84 133: " IF ", # 0x85 ...
Code: Alles auswählen
def samples2bits(samples, framerate, frame_count, even_odd):
in_positive = even_odd
in_negative = not even_odd
toggle_count = 0 # Counter for detect a complete cycle
previous_frame_no = 0
bit_count = 0
window_values = collections.deque(maxlen=MIN_TOGGLE_COUNT)
for frame_no, value in samples:
window_values.append(value)
if len(window_values) >= MIN_TOGGLE_COUNT:
positive_count, negative_count = count_sign(window_values)
# print window_values, positive_count, negative_count
if not in_positive and positive_count == MIN_TOGGLE_COUNT and negative_count == 0:
# go into a positive sinus area
in_positive = True
in_negative = False
toggle_count += 1
elif not in_negative and negative_count == MIN_TOGGLE_COUNT and positive_count == 0:
# go into a negative sinus area
in_negative = True
in_positive = False
toggle_count += 1
if toggle_count >= 2:
# a single sinus cycle complete
toggle_count = 0
frame_count = frame_no - previous_frame_no
hz = framerate / frame_count
dst_one = abs(ONE_HZ - hz)
dst_nul = abs(NUL_HZ - hz)
if dst_one < dst_nul:
bit_count += 1
yield 1
else:
bit_count += 1
yield 0
previous_frame_no = frame_no
Return the number of zero crossings in the fragment passed as an argument.