Skip to content

Commit 2adeaca

Browse files
committed
Add Pi CLI
First supported command is "convert". It converts files from one format into another. With this feature you can convert pico-8 file into Pi formats: sprite-sheet.png and audio.sfx
1 parent 0be3e14 commit 2adeaca

File tree

13 files changed

+570
-0
lines changed

13 files changed

+570
-0
lines changed

go.work

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.20
2+
3+
use (
4+
.
5+
./pi
6+
)

go.work.sum

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
2+
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
3+
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc=
4+
github.com/go-text/typesetting v0.0.0-20230905121921-abdbcca6e0eb h1:4GpJirtA8yY24aqbU3uppiXGYiVpWfLIrqc2NNKKk9s=
5+
github.com/go-text/typesetting v0.0.0-20230905121921-abdbcca6e0eb/go.mod h1:evDBbvNR/KaVFZ2ZlDSOWWXIUKq0wCOEtzLxRM8SG3k=
6+
github.com/hajimehoshi/bitmapfont/v2 v2.2.3 h1:jmq/TMNj352V062Tr5e3hAoipkoxCbY1JWTzor0zNps=
7+
github.com/hajimehoshi/bitmapfont/v2 v2.2.3/go.mod h1:sWM8ejdkGSXaQGlZcegMRx4DyEPOWYyXqsBKIs+Yhzk=
8+
github.com/hajimehoshi/bitmapfont/v3 v3.0.0 h1:r2+6gYK38nfztS/et50gHAswb9hXgxXECYgE8Nczmi4=
9+
github.com/hajimehoshi/bitmapfont/v3 v3.0.0/go.mod h1:+CxxG+uMmgU4mI2poq944i3uZ6UYFfAkj9V6WqmuvZA=
10+
github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68=
11+
github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo=
12+
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lToqnXgA8Mz1DP11X4zSJ159C3k=
13+
github.com/jakecoffman/cp v1.2.1 h1:zkhc2Gpo9l4NLUZfeG3j33+3bQD7MkqPa+n5PdX+5mI=
14+
github.com/jakecoffman/cp v1.2.1/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
15+
github.com/jfreymuth/oggvorbis v1.0.5 h1:u+Ck+R0eLSRhgq8WTmffYnrVtSztJcYrl588DM4e3kQ=
16+
github.com/jfreymuth/oggvorbis v1.0.5/go.mod h1:1U4pqWmghcoVsCJJ4fRBKv9peUJMBHixthRlBeD6uII=
17+
github.com/jfreymuth/vorbis v1.0.2 h1:m1xH6+ZI4thH927pgKD8JOH4eaGRm18rEE9/0WKjvNE=
18+
github.com/jfreymuth/vorbis v1.0.2/go.mod h1:DoftRo4AznKnShRl1GxiTFCseHr4zR9BN3TWXyuzrqQ=
19+
github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
20+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
21+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
22+
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
23+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
24+
golang.org/x/exp/shiny v0.0.0-20230817173708-d852ddb80c63 h1:3AGKexOYqL+ztdWdkB1bDwXgPBuTS/S8A4WzuTvJ8Cg=
25+
golang.org/x/exp/shiny v0.0.0-20230817173708-d852ddb80c63/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0=
26+
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
27+
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
28+
golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
29+
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
30+
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
31+
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
32+
golang.org/x/tools v0.12.1-0.20230818130535-1517d1a3ba60 h1:o4bs4seAAlSiZQAZbO6/RP5XBCZCooQS3Pgc0AUjWts=
33+
golang.org/x/tools v0.12.1-0.20230818130535-1517d1a3ba60/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
34+
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
35+
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
36+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
37+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

pi/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# pi command line interface
2+
3+
## Install
4+
5+
```sh
6+
go install github.com/elgopher/pi/pi
7+
```
8+
9+
## Run
10+
11+
```shell
12+
pi [global options] command [command options] [arguments...]
13+
```

pi/cli.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// (c) 2022-2023 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"os"
9+
10+
"github.com/urfave/cli/v2"
11+
12+
"github.com/elgopher/pi/pi/internal/convert"
13+
)
14+
15+
func main() {
16+
app := cli.App{
17+
Usage: "Pi Command Line Interface",
18+
Commands: []*cli.Command{
19+
convertCmd(),
20+
},
21+
}
22+
err := app.Run(os.Args)
23+
if err != nil {
24+
_, _ = os.Stderr.WriteString(err.Error())
25+
}
26+
}
27+
28+
func convertCmd() *cli.Command {
29+
return &cli.Command{
30+
Name: "convert",
31+
Usage: "Converts one file format into another one",
32+
Description: "Format of input and output file is deducted based on files extension.",
33+
Flags: []cli.Flag{
34+
&cli.StringFlag{
35+
Name: "format",
36+
Aliases: []string{"f"},
37+
Usage: "Format of input file. Overrides what CLI deducted based on input file extension. For now, the only supported input format is p8.",
38+
},
39+
},
40+
ArgsUsage: "input.file output.file",
41+
Action: func(context *cli.Context) error {
42+
inputFile := context.Args().Get(0)
43+
outputFile := context.Args().Get(1)
44+
45+
if context.Args().Len() > 2 {
46+
return fmt.Errorf("too many arguments")
47+
}
48+
49+
command := convert.Command{
50+
InputFormat: convert.InputFormat(context.String("format")),
51+
InputFile: inputFile,
52+
OutputFile: outputFile,
53+
}
54+
return command.Run()
55+
},
56+
}
57+
}

pi/go.mod

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/elgopher/pi/pi
2+
3+
go 1.20
4+
5+
require (
6+
github.com/icza/bitio v1.1.0
7+
github.com/urfave/cli/v2 v2.25.7
8+
)
9+
10+
require (
11+
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
12+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
13+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
14+
)

pi/go.sum

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
2+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3+
github.com/icza/bitio v1.1.0 h1:ysX4vtldjdi3Ygai5m1cWy4oLkhWTAi+SyO6HC8L9T0=
4+
github.com/icza/bitio v1.1.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A=
5+
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lToqnXgA8Mz1DP11X4zSJ159C3k=
6+
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA=
7+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
8+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
9+
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
10+
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
11+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
12+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=

pi/internal/convert/convert.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// (c) 2022-2023 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package convert
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
10+
"github.com/elgopher/pi/pi/internal/convert/internal/p8"
11+
)
12+
13+
type InputFormat string
14+
15+
const (
16+
InputFormatP8 = "p8"
17+
)
18+
19+
type Command struct {
20+
InputFormat
21+
InputFile string
22+
OutputFile string
23+
}
24+
25+
func (o Command) Run() error {
26+
if o.InputFile == "" {
27+
return fmt.Errorf("input file not provided")
28+
}
29+
30+
if o.InputFormat != "" && o.InputFormat != InputFormatP8 {
31+
return fmt.Errorf("input format %s not supported", o.InputFormat)
32+
}
33+
34+
if o.InputFormat == "" {
35+
if strings.HasSuffix(o.InputFile, ".p8") {
36+
o.InputFormat = InputFormatP8
37+
} else {
38+
return fmt.Errorf("cannot deduct the format of %s input file", o.InputFile)
39+
}
40+
}
41+
42+
if o.OutputFile == "" {
43+
return fmt.Errorf("output file not provided")
44+
}
45+
46+
fmt.Printf("Converting %s to %s... ", o.InputFile, o.OutputFile)
47+
fmt.Printf("Using %s input format... ", o.InputFormat)
48+
if err := o.convert(); err != nil {
49+
return err
50+
}
51+
fmt.Println("Done")
52+
return nil
53+
}
54+
55+
func (o Command) convert() error {
56+
if o.InputFormat == InputFormatP8 {
57+
if err := p8.ConvertToAudioSfx(o.InputFile, o.OutputFile); err != nil {
58+
return err
59+
}
60+
}
61+
62+
return nil
63+
}

pi/internal/convert/internal/p8/p8.go

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package p8
2+
3+
import (
4+
"bytes"
5+
"encoding/hex"
6+
"fmt"
7+
"os"
8+
9+
"github.com/icza/bitio"
10+
11+
"github.com/elgopher/pi/audio"
12+
)
13+
14+
func ConvertToAudioSfx(inputFile, outputFile string) error {
15+
parser := Parser{}
16+
file, err := parser.Parse(inputFile)
17+
if err != nil {
18+
return fmt.Errorf("error parsing p8 file %s: %w", inputFile, err)
19+
}
20+
21+
for _, section := range file.Sections {
22+
if section.Name == "__sfx__" {
23+
sfx, err := decodeSfx(section.Lines)
24+
if err != nil {
25+
return fmt.Errorf("error decoding __sfx__ section from p8 file %s: %w", inputFile, err)
26+
}
27+
audio.Sfx = sfx
28+
}
29+
}
30+
31+
bytes, err := audio.Save()
32+
if err != nil {
33+
return fmt.Errorf("saving audio failed: %w", err)
34+
}
35+
36+
err = os.WriteFile(outputFile, bytes, 0644)
37+
if err != nil {
38+
return fmt.Errorf("writing %s failed: %w", outputFile, err)
39+
}
40+
41+
return nil
42+
}
43+
44+
func decodeSfx(lines []string) (sfx [64]audio.SoundEffect, err error) {
45+
// each line is a sound effect
46+
for no, line := range lines {
47+
decoded, err := hex.DecodeString(line)
48+
if err != nil {
49+
return sfx, err
50+
}
51+
52+
notes, err := decodeSfxNotes(decoded[4:])
53+
if err != nil {
54+
return sfx, err
55+
}
56+
57+
editorModeAndFilters := bitio.NewReader(bytes.NewReader(decoded[0:1]))
58+
editorMode, err := editorModeAndFilters.ReadBool()
59+
if err != nil {
60+
return sfx, err
61+
}
62+
_ = editorMode
63+
64+
noiz, err := editorModeAndFilters.ReadBool()
65+
if err != nil {
66+
return sfx, err
67+
}
68+
69+
buzz, err := editorModeAndFilters.ReadBool()
70+
if err != nil {
71+
return sfx, err
72+
}
73+
74+
detune, err := editorModeAndFilters.ReadBits(2)
75+
if err != nil {
76+
return sfx, err
77+
}
78+
79+
reverb, err := editorModeAndFilters.ReadBits(2)
80+
if err != nil {
81+
return sfx, err
82+
}
83+
84+
dampen, err := editorModeAndFilters.ReadBits(2)
85+
if err != nil {
86+
return sfx, err
87+
}
88+
89+
sfx[no] = audio.SoundEffect{
90+
Speed: decoded[1],
91+
LoopStart: decoded[2],
92+
LoopStop: decoded[3],
93+
Notes: notes,
94+
Noiz: noiz,
95+
Buzz: buzz,
96+
Detune: byte(detune),
97+
Reverb: byte(reverb),
98+
Dampen: byte(dampen),
99+
}
100+
}
101+
102+
return
103+
}
104+
105+
func decodeSfxNotes(b []byte) (notes [32]audio.Note, err error) {
106+
reader := bitio.NewReader(bytes.NewBuffer(b))
107+
for i := 0; i < 32; i++ {
108+
pitch, err := reader.ReadByte()
109+
if err != nil {
110+
return notes, err
111+
}
112+
notes[i].Pitch = audio.Pitch(pitch)
113+
114+
waveform, err := reader.ReadBits(4)
115+
if err != nil {
116+
return notes, err
117+
}
118+
notes[i].Instrument = audio.Instrument(waveform)
119+
120+
volume, err := reader.ReadBits(4)
121+
if err != nil {
122+
return notes, err
123+
}
124+
notes[i].Volume = audio.Volume(volume)
125+
126+
effect, err := reader.ReadBits(4)
127+
if err != nil {
128+
return notes, err
129+
}
130+
notes[i].Effect = audio.Effect(effect)
131+
}
132+
133+
return
134+
}

0 commit comments

Comments
 (0)