-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.go
150 lines (139 loc) · 3.19 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (c) 2016 - 2017 Andreas Signer <asigner@gmail.com>
*
* This file is part of kosmos_tape_tool.
*
* kosmos_tape_tool is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* kosmos_tape_tool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with kosmos_tape_tool. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"log"
"os"
)
type TapeReader struct {
binName string
wavName string
}
func tonesToBits(toneChannel chan Tone, bitChannel chan uint) {
// wait for the signal tone (16 secs low frequency)
for {
tone := <-toneChannel
if tone.Freq == FreqLow && tone.Duration > 15 {
break
}
}
// Now, read bits
cnt := 0
for {
tone1, more := <-toneChannel
if !more {
break
}
tone2, more := <-toneChannel
if !more {
break
}
cnt++
if tone1.Freq != FreqHigh || tone2.Freq != FreqLow {
log.Printf("Unrecognized data, quitting")
break
}
var bit uint
if tone1.Duration > tone2.Duration {
bit = 0
} else {
bit = 1
}
log.Printf("Bit %04d: %d", cnt, bit)
bitChannel <- bit
}
close(bitChannel)
log.Printf("DONE")
}
func bitsToBytes(bitChannel chan uint, byteChannel chan byte) {
var cnt uint = 0
var cur byte = 0
for {
bit, more := <-bitChannel
if !more {
log.Printf("No more bits to read")
break
}
if bit > 0 {
cur |= 1 << cnt
}
cnt++
if cnt == 8 {
byteChannel <- cur
cur = 0
cnt = 0
}
}
close(byteChannel)
}
func bytesToFile(byteChannel chan byte, binName string) {
f, err := os.Create(binName)
checkErr(err)
defer f.Close()
cnt := 0
for {
b, more := <-byteChannel
if !more {
log.Printf("No more bytes to read")
break
}
log.Printf("Byte %03d: %03d", cnt, b)
f.Write([]byte{b})
cnt++
}
log.Printf("Wrote %d bytes to file %s.", cnt, binName)
}
func reorderBytes(byteChannel chan byte, reorderedByteChannel chan byte) {
// Kosmos CP1 writes bytes in this order: 0, 254, 253, ..., 1
buf := make([]byte, 256)
pos := 0
block := 0
for {
b, more := <-byteChannel
if !more {
log.Printf("No more bytes to read")
break
}
buf[pos] = b
pos++
if pos == 256 {
log.Printf("Reordering Block %d", block)
block++
reorderedByteChannel <- buf[0]
for i := 0; i < 255; i++ {
reorderedByteChannel <- buf[255-i]
}
pos = 0
}
}
log.Printf("Reordered %d blocks", block)
close(reorderedByteChannel)
}
func (self *TapeReader) convert() {
toneChannel := make(chan Tone)
bitChannel := make(chan uint)
byteChannel := make(chan byte)
reorderedByteChannel := make(chan byte)
wavReader := NewWavReader(self.wavName, toneChannel)
go wavReader.Read()
go tonesToBits(toneChannel, bitChannel)
go bitsToBytes(bitChannel, byteChannel)
go reorderBytes(byteChannel, reorderedByteChannel)
bytesToFile(reorderedByteChannel, self.binName)
}