Skip to content

Commit

Permalink
improved pcm error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Binozo committed Dec 26, 2023
1 parent 53f6a91 commit f412c24
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 8 additions & 4 deletions internal/tinyapi/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ func (d *PcmDevice) BitsPerSample() uint16 {
}

func (d *PcmDevice) GetError() error {
p := C.pcm_get_error(d.pcmDevice)
s := C.GoString(p)
if s == "" {
message := d.getErrorMsg()
if message == "" {
// Check if device is ready
if !d.IsReady() {
return errors.New("device is not ready")
}
}
return errors.New(s)
return errors.New(message)
}

func (d *PcmDevice) getErrorMsg() string {
p := C.pcm_get_error(d.pcmDevice)
return C.GoString(p)
}

func (d *PcmDevice) IsReady() bool {
Expand Down
5 changes: 5 additions & 0 deletions internal/tinyapi/pcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func PcmOpen(cardNr int, deviceNr int, openFlags int, config pcm.Config) (PcmDev

// Check if device is ready
if !device.IsReady() {
errorMsg := device.getErrorMsg()
if errorMsg == "" {
// No real error occurred. Continue as normal
return device, nil
}
return PcmDevice{}, device.GetError()
}
return device, nil
Expand Down

0 comments on commit f412c24

Please sign in to comment.