-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.go
184 lines (140 loc) · 3.25 KB
/
lib.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package tzx
import (
"container/list"
"errors"
"os"
"path/filepath"
"strings"
)
const MaxSampleValue = 32767
type block interface {
getDescription() string
generate(stream audioStream)
getBlock24() *tzx_block_24
getBlock25() *tzx_block_25
}
type baseBlock struct {
description string
}
type audioStream interface {
addEdge(len int)
continuePrevious(len int)
setLevel(level bool, len int)
addPause(lenMs int)
}
type SamplesWriter interface {
WriteSample(sample int16)
Flush()
}
type astream struct {
cpuFreq uint64
freq uint64
cpuTimeStamp uint64
sndTimeStamp uint64
currentLevel bool
cpuTimeBase uint64
sndTimeBase uint64
wr SamplesWriter
}
type context struct {
blocks *list.List
loopLabel *list.Element
loopCounter int
}
func (b *baseBlock) getDescription() string {
return b.description
}
func (b *baseBlock) generate(stream audioStream) {
}
func (b *baseBlock) getBlock24() *tzx_block_24 {
return nil
}
func (b *baseBlock) getBlock25() *tzx_block_25 {
return nil
}
func (s *astream) appendLevel(len int, lvl int16) {
s.cpuTimeStamp += uint64(len) * s.cpuTimeBase
for s.sndTimeStamp < s.cpuTimeStamp {
s.wr.WriteSample(lvl)
s.sndTimeStamp += s.sndTimeBase
}
}
func (s *astream) appendLevelBool(len int, level bool) {
// Generated samples ALWAYS is 16 bit signed. Levels MUST be LOW:-32767, HIGH:32767
// BitstreamWriter will convert its to desired format later
lvl := int16(-MaxSampleValue)
if level {
lvl = -lvl
}
s.appendLevel(len, lvl)
}
func (s *astream) addEdge(len int) {
s.appendLevelBool(len, s.currentLevel)
s.currentLevel = !s.currentLevel
}
func (s *astream) continuePrevious(len int) {
s.appendLevelBool(len, s.currentLevel)
}
func (s *astream) setLevel(level bool, len int) {
s.appendLevelBool(len, s.currentLevel)
s.currentLevel = level
}
func (s *astream) addPause(lenMs int) {
if lenMs == 0 {
return
}
s.appendLevelBool(int(uint64(lenMs)*(s.cpuFreq/1000)), s.currentLevel)
s.currentLevel = false
}
func OpenFile(fileName string) (*context, error) {
c := context{}
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()
fext := strings.ToLower(filepath.Ext(fileName))
if fext == ".tap" {
c.blocks = openTAP(file)
} else if fext == ".tzx" {
c.blocks = openTZX(file)
} else {
return nil, errors.New("unknown file format")
}
return &c, nil
}
func (c *context) GenerateAudioTo(writer SamplesWriter, freq int, trace func(string)) {
stream := astream{freq: uint64(freq), cpuFreq: 3500000, currentLevel: false, wr: writer}
timeBase := getLCM(uint32(stream.freq), uint32(stream.cpuFreq))
stream.cpuTimeBase = timeBase / stream.cpuFreq
stream.sndTimeBase = timeBase / stream.freq
for e := c.blocks.Front(); e != nil; {
b := e.Value.(block)
loopStart := b.getBlock24()
if loopStart != nil {
// loop start
e = e.Next()
c.loopLabel = e
c.loopCounter = int(loopStart.loops_count)
continue
}
loopEnd := b.getBlock25()
if loopEnd != nil {
// loop end
c.loopCounter -= 1
if c.loopCounter == 0 {
e = e.Next()
} else {
e = c.loopLabel
}
continue
}
dscr := b.getDescription()
if trace != nil && dscr != "" {
trace(dscr)
}
b.generate(&stream)
e = e.Next()
}
writer.Flush()
}