Skip to content

Commit

Permalink
Several improvements
Browse files Browse the repository at this point in the history
- Replace usage of deprecated "io/ioutil" package with equivalent methods
  from "io" or "os" package.
- Simplify power amp code to no longer supply IDs for compiled filters.
  Filters are regenerated synchronously for some time in order to avoid
  race conditions, especially during batch processing, so these IDs are
  no longer needed in order to guarantee sequential ordering.
- Change interface of metronome value setters to support them failing to
  set a value. (This is an interface change in preparation for upcoming
  features.)
- Introduce new constants for default values in metronome.
- Update to latest version of "pure-knob", improving support for high-DPI
  displays.
- Several small bugfixes, code improvements and UI changes.
- Increment patch version number: v1.6.6 --> v1.6.7
  • Loading branch information
andrepxx committed Mar 17, 2022
1 parent 9846da8 commit 128463b
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 94 deletions.
162 changes: 119 additions & 43 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/andrepxx/go-dsp-guitar/tuner"
"github.com/andrepxx/go-dsp-guitar/wave"
"github.com/andrepxx/go-dsp-guitar/webserver"
"io/ioutil"
"io"
"math"
"os"
"runtime"
Expand Down Expand Up @@ -340,17 +340,18 @@ func (this *controllerStruct) getConfigurationHandler(request webserver.HttpRequ
fx := this.effects
numChannels := len(fx)
framesPerPeriod := uint32(0)
binding := this.binding

/*
* If we are bound to a hardware interface, query frames per period.
*/
if this.binding != nil {
if binding != nil {
framesPerPeriod = hwio.FramesPerPeriod()
}

webChains := make([]webChainStruct, numChannels)
spatChannels := make([]webSpatializerChannelStruct, numChannels)
paramTypes := effects.ParameterTypes()
parameterTypes := effects.ParameterTypes()

/*
* Iterate over the channels and the associated signal chains.
Expand All @@ -365,33 +366,54 @@ func (this *controllerStruct) getConfigurationHandler(request webserver.HttpRequ
for idUnit := 0; idUnit < numUnits; idUnit++ {
unitType, _ := chain.UnitType(idUnit)
bypass, _ := chain.GetBypass(idUnit)
params, _ := chain.Parameters(idUnit)
numParams := len(params)
webParams := make([]webParameterStruct, numParams)
parameters, _ := chain.Parameters(idUnit)
numParameters := len(parameters)
webParameters := make([]webParameterStruct, numParameters)

/*
* Iterate over the parameters.
*/
for idParameter, parameter := range parameters {
name := parameter.Name
parameterTypeId := parameter.Type
parameterType := parameterTypes[parameterTypeId]
physicalUnit := parameter.PhysicalUnit
minimum := parameter.Minimum
maximum := parameter.Maximum
numericValue := parameter.NumericValue
discreteValueIndex := parameter.DiscreteValueIndex
discreteValuesSource := parameter.DiscreteValues
numDiscreteValues := len(discreteValuesSource)
discreteValues := make([]string, numDiscreteValues)
copy(discreteValues, discreteValuesSource)

/*
* Create data structure for parameter.
*/
webParameter := webParameterStruct{
Name: name,
Type: parameterType,
PhysicalUnit: physicalUnit,
Minimum: minimum,
Maximum: maximum,
NumericValue: numericValue,
DiscreteValueIndex: discreteValueIndex,
DiscreteValues: discreteValues,
}

webParameters[idParameter] = webParameter
}

/*
* Iterate over the parameters and copy all values.
* Create data structure for unit.
*/
for idParam, param := range params {
paramTypeId := param.Type
paramType := paramTypes[paramTypeId]
webParams[idParam].Name = param.Name
webParams[idParam].Type = paramType
webParams[idParam].PhysicalUnit = param.PhysicalUnit
webParams[idParam].Minimum = param.Minimum
webParams[idParam].Maximum = param.Maximum
webParams[idParam].NumericValue = param.NumericValue
webParams[idParam].DiscreteValueIndex = param.DiscreteValueIndex
nValues := len(param.DiscreteValues)
discreteValuesSource := param.DiscreteValues
discreteValuesTarget := make([]string, nValues)
copy(discreteValuesTarget, discreteValuesSource)
webParams[idParam].DiscreteValues = discreteValuesTarget
webUnit := webUnitStruct{
Type: unitType,
Bypass: bypass,
Parameters: webParameters,
}

webUnits[idUnit].Type = unitType
webUnits[idUnit].Bypass = bypass
webUnits[idUnit].Parameters = webParams
webUnits[idUnit] = webUnit
}

webChains[idChannel].Units = webUnits
Expand All @@ -403,11 +425,19 @@ func (this *controllerStruct) getConfigurationHandler(request webserver.HttpRequ
if spat != nil {
idChannel32 := uint32(idChannel)
azimuth, _ := spat.GetAzimuth(idChannel32)
spatChannels[idChannel].Azimuth = azimuth
distance, _ := spat.GetDistance(idChannel32)
spatChannels[idChannel].Distance = distance
level, _ := spat.GetLevel(idChannel32)
spatChannels[idChannel].Level = level

/*
* Create data structure for spatializer channel.
*/
spatChannel := webSpatializerChannelStruct{
Azimuth: azimuth,
Distance: distance,
Level: level,
}

spatChannels[idChannel] = spatChannel
}

}
Expand Down Expand Up @@ -474,7 +504,7 @@ func (this *controllerStruct) getConfigurationHandler(request webserver.HttpRequ
Enabled: levelMeterEnabled,
}

batchProcessing := (this.binding == nil)
batchProcessing := (binding == nil)

/*
* Create configuration structure.
Expand Down Expand Up @@ -895,7 +925,7 @@ func (this *controllerStruct) persistenceRestoreHandler(request webserver.HttpRe

} else {
patchFile := patchFiles[0]
patchBytes, err := ioutil.ReadAll(patchFile)
patchBytes, err := io.ReadAll(patchFile)

/*
* Check if patch file could be successfully read.
Expand Down Expand Up @@ -2061,12 +2091,12 @@ func (this *controllerStruct) setMetronomeValueHandler(request webserver.HttpReq
*/
switch param {
case "beats-per-period":
rawValue, err := strconv.ParseUint(value, 10, 32)
rawValue, errParse := strconv.ParseUint(value, 10, 32)

/*
* Check if value failed to parse.
*/
if err != nil {
if errParse != nil {

/*
* Indicate failure.
Expand All @@ -2078,14 +2108,33 @@ func (this *controllerStruct) setMetronomeValueHandler(request webserver.HttpReq

} else {
value := uint32(rawValue)
metr.SetBeatsPerPeriod(value)
errSet := metr.SetBeatsPerPeriod(value)

/*
* Indicate success.
* Check if value could be set.
*/
webResponse = webResponseStruct{
Success: true,
Reason: "",
if errSet != nil {
msg := errSet.Error()
reason := fmt.Sprintf("Failed to set metronome beats per period: %s", msg)

/*
* Indicate failure.
*/
webResponse = webResponseStruct{
Success: false,
Reason: reason,
}

} else {

/*
* Indicate success.
*/
webResponse = webResponseStruct{
Success: true,
Reason: "",
}

}

}
Expand Down Expand Up @@ -2120,12 +2169,12 @@ func (this *controllerStruct) setMetronomeValueHandler(request webserver.HttpReq
}

case "speed":
rawValue, err := strconv.ParseUint(value, 10, 32)
rawValue, errParse := strconv.ParseUint(value, 10, 32)

/*
* Check if value failed to parse.
*/
if err != nil {
if errParse != nil {

/*
* Indicate failure.
Expand All @@ -2137,7 +2186,34 @@ func (this *controllerStruct) setMetronomeValueHandler(request webserver.HttpReq

} else {
value := uint32(rawValue)
metr.SetSpeed(value)
errSet := metr.SetSpeed(value)

/*
* Check if value could be set.
*/
if errSet != nil {
msg := errSet.Error()
reason := fmt.Sprintf("Failed to set metronome speed: %s", msg)

/*
* Indicate failure.
*/
webResponse = webResponseStruct{
Success: false,
Reason: reason,
}

} else {

/*
* Indicate success.
*/
webResponse = webResponseStruct{
Success: true,
Reason: "",
}

}

/*
* Indicate success.
Expand Down Expand Up @@ -2819,7 +2895,7 @@ func (this *controllerStruct) processFiles(scanner *bufio.Scanner, targetRate ui
inputs[fileId] = make([]float64, 0)
sampleRates[fileId] = DEFAULT_SAMPLE_RATE
} else {
buf, err := ioutil.ReadFile(fileName)
buf, err := os.ReadFile(fileName)

/*
* Check if file could be read.
Expand Down Expand Up @@ -3156,7 +3232,7 @@ func (this *controllerStruct) processFiles(scanner *bufio.Scanner, targetRate ui
* Initialize the controller.
*/
func (this *controllerStruct) initialize(nInputs uint32, useHardware bool) error {
content, err := ioutil.ReadFile(CONFIG_PATH)
content, err := os.ReadFile(CONFIG_PATH)

/*
* Check if file could be read.
Expand Down Expand Up @@ -3442,7 +3518,7 @@ func CreateController() Controller {
* Returns version information.
*/
func Version() (string, error) {
content, err := ioutil.ReadFile(CONFIG_PATH)
content, err := os.ReadFile(CONFIG_PATH)

/*
* Check if file could be read.
Expand Down
17 changes: 6 additions & 11 deletions effects/poweramp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type poweramp struct {
/*
* Compile a new filter for this power amplifier.
*/
func (this *poweramp) compile(sampleRate uint32, id uint64) (filter.Filter, error) {
func (this *poweramp) compile(sampleRate uint32) (filter.Filter, error) {
irs := this.impulseResponses

/*
Expand Down Expand Up @@ -115,7 +115,8 @@ func (this *poweramp) compile(sampleRate uint32, id uint64) (filter.Filter, erro
* Check for errors.
*/
if err != nil {
return nil, fmt.Errorf("Failed to add filter: %s", err.Error())
msg := err.Error()
return nil, fmt.Errorf("Failed to add filter: %s", msg)
}

}
Expand All @@ -137,9 +138,7 @@ func (this *poweramp) SetDiscreteValue(name string, value string) error {
*/
if err == nil {
sr := this.sampleRate
id := this.idCompiled + 1
this.idCompiled = id
flt, err := this.compile(sr, id)
flt, err := this.compile(sr)

/*
* Check if filter was compiled.
Expand All @@ -166,9 +165,7 @@ func (this *poweramp) SetNumericValue(name string, value int32) error {
*/
if err == nil {
sr := this.sampleRate
id := this.idCompiled + 1
this.idCompiled = id
flt, err := this.compile(sr, id)
flt, err := this.compile(sr)

/*
* Check if filter was compiled.
Expand All @@ -194,9 +191,7 @@ func (this *poweramp) Process(in []float64, out []float64, sampleRate uint32) {
if sampleRate != this.sampleRate {
this.sampleRate = sampleRate
sr := this.sampleRate
id := this.idCompiled + 1
this.idCompiled = id
flt, err := this.compile(sr, id)
flt, err := this.compile(sr)

/*
* Check if filter was compiled.
Expand Down
6 changes: 3 additions & 3 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/andrepxx/go-dsp-guitar/fft"
"github.com/andrepxx/go-dsp-guitar/resample"
"github.com/andrepxx/go-dsp-guitar/wave"
"io/ioutil"
"math"
"math/cmplx"
"os"
"strconv"
)

Expand Down Expand Up @@ -702,7 +702,7 @@ func (this *impulseResponsesStruct) Names() []string {
* Imports a set of impulse responses using a descriptor file.
*/
func Import(descriptorFilePath string) (ImpulseResponses, error) {
content, err := ioutil.ReadFile(descriptorFilePath)
content, err := os.ReadFile(descriptorFilePath)

/*
* Check if file could be read.
Expand Down Expand Up @@ -732,7 +732,7 @@ func Import(descriptorFilePath string) (ImpulseResponses, error) {
dcFloat := float64(dc)
compensation := 0.05 * dcFloat
fac := math.Pow(10.0, compensation)
waveBuffer, err := ioutil.ReadFile(wavePath)
waveBuffer, err := os.ReadFile(wavePath)

/*
* Check if file was read successfully.
Expand Down
Loading

0 comments on commit 128463b

Please sign in to comment.