Renaming to a more appropriate name
This commit is contained in:
parent
d0f7d1a7f8
commit
56d44e6f23
|
@ -33,9 +33,9 @@ Coming soon!
|
|||
|
||||
Same general code as vidplayback but buffers the ENTIRE file to allow for seeking. I do NOT recommend this for general playback but for some purposes, this will suffice.
|
||||
|
||||
## Audio Spectrum
|
||||
## Audio Waveform
|
||||
|
||||
A widget to create an audio spectrum plot of []int32 data
|
||||
A widget to display an audio waveform plot of []int32 data
|
||||
|
||||
|Field|Type|Effect|Values|
|
||||
|---|---|---|---|
|
||||
|
|
|
@ -9,24 +9,24 @@ import (
|
|||
"fyne.io/fyne/v2/canvas"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"git.martyn.berlin/martyn/fyne-widgets/pkg/audiospectrum"
|
||||
"git.martyn.berlin/martyn/fyne-widgets/pkg/layouts"
|
||||
"git.martyn.berlin/martyn/fyne-widgets/pkg/waveform"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := app.New()
|
||||
w := a.NewWindow("VUMeter")
|
||||
w := a.NewWindow("Waveform")
|
||||
randomAudioData := make([]int32, 1200)
|
||||
for i := 0; i < 1200; i++ {
|
||||
randomAudioData[i] = rand.Int31()
|
||||
}
|
||||
as := audiospectrum.NewAudioSpectrum(randomAudioData)
|
||||
as2 := audiospectrum.NewAudioSpectrum(randomAudioData)
|
||||
as := waveform.NewWaveform(randomAudioData)
|
||||
as2 := waveform.NewWaveform(randomAudioData)
|
||||
as2.OverrideBackground = true
|
||||
as2.OverrideBackgroundColor = color.RGBA{0, 0, 0xff, 0xff} // no effect because of the next line
|
||||
as2.TransparentBackground = true
|
||||
as2.SetMinSize(fyne.NewSize(1200, 64))
|
||||
as3 := audiospectrum.NewAudioSpectrum(randomAudioData)
|
||||
as3 := waveform.NewWaveform(randomAudioData)
|
||||
as3.StretchSamples = true
|
||||
as3.SetMinSize(fyne.NewSize(100, 64))
|
||||
as3.OverrideForeground = true
|
|
@ -1,4 +1,4 @@
|
|||
package audiospectrum
|
||||
package waveform
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
@ -17,7 +17,7 @@ import (
|
|||
//
|
||||
// A visualisation of an audio stream widget with themed or overridden background and foreground
|
||||
//
|
||||
type AudioSpectrum struct {
|
||||
type Waveform struct {
|
||||
widget.BaseWidget // Inherit from BaseWidget
|
||||
audioData []int32 // The data to display in the widget
|
||||
StretchSamples bool
|
||||
|
@ -32,8 +32,8 @@ type AudioSpectrum struct {
|
|||
//
|
||||
// Create a Widget and Extend (initialiase) the BaseWidget
|
||||
//
|
||||
func NewAudioSpectrum(data []int32) *AudioSpectrum {
|
||||
w := &AudioSpectrum{ // Create this widget with an initial text value
|
||||
func NewWaveform(data []int32) *Waveform {
|
||||
w := &Waveform{ // Create this widget with an initial text value
|
||||
audioData: data,
|
||||
StretchSamples: false,
|
||||
TransparentBackground: false,
|
||||
|
@ -50,15 +50,15 @@ func NewAudioSpectrum(data []int32) *AudioSpectrum {
|
|||
//
|
||||
// Create the renderer. This is called by the fyne application
|
||||
//
|
||||
func (w *AudioSpectrum) CreateRenderer() fyne.WidgetRenderer {
|
||||
func (w *Waveform) CreateRenderer() fyne.WidgetRenderer {
|
||||
// Pass this widget to the renderer so it can access the text field
|
||||
return newAudioSpectrumRenderer(w)
|
||||
return newWaveformRenderer(w)
|
||||
}
|
||||
|
||||
//
|
||||
// Set the minsize (default is 200x64)
|
||||
//
|
||||
func (w *AudioSpectrum) SetMinSize(newSize fyne.Size) {
|
||||
func (w *Waveform) SetMinSize(newSize fyne.Size) {
|
||||
// Pass this widget to the renderer so it can access the text field
|
||||
w.minSize = newSize
|
||||
}
|
||||
|
@ -66,8 +66,8 @@ func (w *AudioSpectrum) SetMinSize(newSize fyne.Size) {
|
|||
//
|
||||
// Widget Renderer code starts here
|
||||
//
|
||||
type audioSpectrumRenderer struct {
|
||||
widget *AudioSpectrum // Reference to the widget holding the current state
|
||||
type waveformRenderer struct {
|
||||
widget *Waveform // Reference to the widget holding the current state
|
||||
background *canvas.Rectangle // A background rectangle
|
||||
canvas *canvas.Raster // The waveform
|
||||
}
|
||||
|
@ -78,8 +78,8 @@ type audioSpectrumRenderer struct {
|
|||
//
|
||||
// Do not size or move canvas objects here.
|
||||
//
|
||||
func newAudioSpectrumRenderer(theWidget *AudioSpectrum) *audioSpectrumRenderer {
|
||||
r := &audioSpectrumRenderer{
|
||||
func newWaveformRenderer(theWidget *Waveform) *waveformRenderer {
|
||||
r := &waveformRenderer{
|
||||
widget: theWidget,
|
||||
background: canvas.NewRectangle(theme.BackgroundColor()),
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ func int32Map(x int32, in_min int32, in_max int32, out_min int32, out_max int32)
|
|||
return int32(r)
|
||||
}
|
||||
|
||||
func (r *audioSpectrumRenderer) audioDataToImage(w, h int) image.Image {
|
||||
func (r *waveformRenderer) audioDataToImage(w, h int) image.Image {
|
||||
foregroundColor := theme.ForegroundColor()
|
||||
if r.widget.OverrideForeground {
|
||||
foregroundColor = r.widget.OverrideForegroundColor
|
||||
|
@ -113,7 +113,7 @@ func (r *audioSpectrumRenderer) audioDataToImage(w, h int) image.Image {
|
|||
i := int32(0)
|
||||
for i = 0; i < int32(len(r.widget.audioData)); i++ {
|
||||
sample := r.widget.audioData[i]
|
||||
// /2 because we draw spectrums with 0 at the middle!
|
||||
// /2 because we draw waveforms with 0 at the middle!
|
||||
sampleheight := int32Map(sample, 0, math.MaxInt32, 0, int32(h/2))
|
||||
y := int32(0)
|
||||
//center line
|
||||
|
@ -137,7 +137,7 @@ func (r *audioSpectrumRenderer) audioDataToImage(w, h int) image.Image {
|
|||
//
|
||||
// Note: The background and foreground colours are set from the current theme
|
||||
//
|
||||
func (r *audioSpectrumRenderer) Refresh() {
|
||||
func (r *waveformRenderer) Refresh() {
|
||||
backgroundColor := theme.BackgroundColor()
|
||||
if r.widget.OverrideBackground {
|
||||
backgroundColor = color.RGBA{0, 0, 0xff, 0xff} //r.widget.OverrideBackgroundColor
|
||||
|
@ -151,7 +151,7 @@ func (r *audioSpectrumRenderer) Refresh() {
|
|||
// Given the size required by the fyne application move and re-size the
|
||||
// canvas objects.
|
||||
//
|
||||
func (r *audioSpectrumRenderer) Layout(s fyne.Size) {
|
||||
func (r *waveformRenderer) Layout(s fyne.Size) {
|
||||
// Make sure the background fills the widget
|
||||
r.background.Resize(s)
|
||||
r.canvas.Resize(s)
|
||||
|
@ -162,7 +162,7 @@ func (r *audioSpectrumRenderer) Layout(s fyne.Size) {
|
|||
// Create a minimum size for the widget.
|
||||
// The smallest size is the size of the text with a border defined by the theme padding
|
||||
//
|
||||
func (r *audioSpectrumRenderer) MinSize() fyne.Size {
|
||||
func (r *waveformRenderer) MinSize() fyne.Size {
|
||||
// Default is set in newSpectrumRenderer, overridable by users
|
||||
return r.widget.minSize
|
||||
}
|
||||
|
@ -170,7 +170,7 @@ func (r *audioSpectrumRenderer) MinSize() fyne.Size {
|
|||
//
|
||||
// Return a list of each canvas object.
|
||||
//
|
||||
func (r *audioSpectrumRenderer) Objects() []fyne.CanvasObject {
|
||||
func (r *waveformRenderer) Objects() []fyne.CanvasObject {
|
||||
if r.widget.TransparentBackground {
|
||||
return []fyne.CanvasObject{r.canvas}
|
||||
}
|
||||
|
@ -180,4 +180,4 @@ func (r *audioSpectrumRenderer) Objects() []fyne.CanvasObject {
|
|||
//
|
||||
// Cleanup if resources have been allocated
|
||||
//
|
||||
func (r *audioSpectrumRenderer) Destroy() {}
|
||||
func (r *waveformRenderer) Destroy() {}
|
Loading…
Reference in New Issue