Skip to content

Commit

Permalink
Add Pi CLI
Browse files Browse the repository at this point in the history
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
  • Loading branch information
elgopher committed Oct 23, 2023
1 parent 0be3e14 commit 98f95dd
Show file tree
Hide file tree
Showing 13 changed files with 570 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/pi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# pi command line interface

## Install

```sh
go install github.com/elgopher/pi/cmd/pi
```

## Run

```shell
pi [global options] command [command options] [arguments...]
```
57 changes: 57 additions & 0 deletions cmd/pi/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// (c) 2022-2023 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)

package main

import (
"fmt"
"os"

"github.com/urfave/cli/v2"

"github.com/elgopher/pi/cmd/pi/internal/convert"
)

func main() {
app := cli.App{
Usage: "Pi Command Line Interface",
Commands: []*cli.Command{
convertCmd(),
},
}
err := app.Run(os.Args)
if err != nil {
_, _ = os.Stderr.WriteString(err.Error())
}
}

func convertCmd() *cli.Command {
return &cli.Command{
Name: "convert",
Usage: "Converts one file format into another one",
Description: "Format of input and output file is deducted based on files extension.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
Usage: "Format of input file. Overrides what CLI deducted based on input file extension. For now, the only supported input format is p8.",
},
},
ArgsUsage: "input.file output.file",
Action: func(context *cli.Context) error {
inputFile := context.Args().Get(0)
outputFile := context.Args().Get(1)

if context.Args().Len() > 2 {
return fmt.Errorf("too many arguments")
}

command := convert.Command{
InputFormat: convert.InputFormat(context.String("format")),
InputFile: inputFile,
OutputFile: outputFile,
}
return command.Run()
},
}
}
14 changes: 14 additions & 0 deletions cmd/pi/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/elgopher/pi/cmd/pi

go 1.20

require (
github.com/icza/bitio v1.1.0
github.com/urfave/cli/v2 v2.25.7
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)
12 changes: 12 additions & 0 deletions cmd/pi/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/icza/bitio v1.1.0 h1:ysX4vtldjdi3Ygai5m1cWy4oLkhWTAi+SyO6HC8L9T0=
github.com/icza/bitio v1.1.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A=
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lToqnXgA8Mz1DP11X4zSJ159C3k=
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
63 changes: 63 additions & 0 deletions cmd/pi/internal/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// (c) 2022-2023 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)

package convert

import (
"fmt"
"strings"

"github.com/elgopher/pi/cmd/pi/internal/convert/internal/p8"
)

type InputFormat string

const (
InputFormatP8 = "p8"
)

type Command struct {
InputFormat
InputFile string
OutputFile string
}

func (o Command) Run() error {
if o.InputFile == "" {
return fmt.Errorf("input file not provided")
}

if o.InputFormat != "" && o.InputFormat != InputFormatP8 {
return fmt.Errorf("input format %s not supported", o.InputFormat)
}

if o.InputFormat == "" {
if strings.HasSuffix(o.InputFile, ".p8") {
o.InputFormat = InputFormatP8
} else {
return fmt.Errorf("cannot deduct the format of %s input file", o.InputFile)
}
}

if o.OutputFile == "" {
return fmt.Errorf("output file not provided")
}

fmt.Printf("Converting %s to %s... ", o.InputFile, o.OutputFile)
fmt.Printf("Using %s input format... ", o.InputFormat)
if err := o.convert(); err != nil {
return err
}
fmt.Println("Done")
return nil
}

func (o Command) convert() error {
if o.InputFormat == InputFormatP8 {
if err := p8.ConvertToAudioSfx(o.InputFile, o.OutputFile); err != nil {
return err
}
}

return nil
}
134 changes: 134 additions & 0 deletions cmd/pi/internal/convert/internal/p8/p8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package p8

import (
"bytes"
"encoding/hex"
"fmt"
"os"

"github.com/icza/bitio"

"github.com/elgopher/pi/audio"
)

func ConvertToAudioSfx(inputFile, outputFile string) error {
parser := Parser{}
file, err := parser.Parse(inputFile)
if err != nil {
return fmt.Errorf("error parsing p8 file %s: %w", inputFile, err)
}

for _, section := range file.Sections {
if section.Name == "__sfx__" {
sfx, err := decodeSfx(section.Lines)
if err != nil {
return fmt.Errorf("error decoding __sfx__ section from p8 file %s: %w", inputFile, err)
}
audio.Sfx = sfx
}
}

bytes, err := audio.Save()
if err != nil {
return fmt.Errorf("saving audio failed: %w", err)
}

err = os.WriteFile(outputFile, bytes, 0644)
if err != nil {
return fmt.Errorf("writing %s failed: %w", outputFile, err)
}

return nil
}

func decodeSfx(lines []string) (sfx [64]audio.SoundEffect, err error) {
// each line is a sound effect
for no, line := range lines {
decoded, err := hex.DecodeString(line)
if err != nil {
return sfx, err
}

notes, err := decodeSfxNotes(decoded[4:])
if err != nil {
return sfx, err
}

editorModeAndFilters := bitio.NewReader(bytes.NewReader(decoded[0:1]))
editorMode, err := editorModeAndFilters.ReadBool()
if err != nil {
return sfx, err
}
_ = editorMode

noiz, err := editorModeAndFilters.ReadBool()
if err != nil {
return sfx, err
}

buzz, err := editorModeAndFilters.ReadBool()
if err != nil {
return sfx, err
}

detune, err := editorModeAndFilters.ReadBits(2)
if err != nil {
return sfx, err
}

reverb, err := editorModeAndFilters.ReadBits(2)
if err != nil {
return sfx, err
}

dampen, err := editorModeAndFilters.ReadBits(2)
if err != nil {
return sfx, err
}

sfx[no] = audio.SoundEffect{
Speed: decoded[1],
LoopStart: decoded[2],
LoopStop: decoded[3],
Notes: notes,
Noiz: noiz,
Buzz: buzz,
Detune: byte(detune),
Reverb: byte(reverb),
Dampen: byte(dampen),
}
}

return
}

func decodeSfxNotes(b []byte) (notes [32]audio.Note, err error) {
reader := bitio.NewReader(bytes.NewBuffer(b))
for i := 0; i < 32; i++ {
pitch, err := reader.ReadByte()
if err != nil {
return notes, err
}
notes[i].Pitch = audio.Pitch(pitch)

waveform, err := reader.ReadBits(4)
if err != nil {
return notes, err
}
notes[i].Instrument = audio.Instrument(waveform)

volume, err := reader.ReadBits(4)
if err != nil {
return notes, err
}
notes[i].Volume = audio.Volume(volume)

effect, err := reader.ReadBits(4)
if err != nil {
return notes, err
}
notes[i].Effect = audio.Effect(effect)
}

return
}
Loading

0 comments on commit 98f95dd

Please sign in to comment.