Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pi cli #119

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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