Skip to content

Commit

Permalink
Merge pull request #86 from ilosvideos/master
Browse files Browse the repository at this point in the history
Add an option to draw frequency meter from the middle out rather than left to right
  • Loading branch information
staskobzar committed Dec 10, 2021
2 parents 6244863 + a928c2a commit 7cb91a7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,15 @@ export default {
Example: <code>:frequ-line-cap="true"</code>
</td>
</tr>
<tr>
<td>frequ-direction</td>
<td><code>String</code></td>
<td><code>lr</code></td>
<td>Direction to draw the frequency. Available values: 'lr' or 'mo' (left to right or middle out).
If not set or not recognized then 'lr' is set. <br/>
Example: <code>frequ-direction="mo"</code>
</td>
</tr>
<tr>
<td>line-color</td>
<td><code>String</code></td>
Expand Down
4 changes: 4 additions & 0 deletions demo/src/components/AvMedia.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<audio ref="player"></audio>
<small>AvMedia type="frequ"</small>
<av-media type="frequ" :media="media" line-color="darkorange"/>
<small>AvMedia type="frequ" with frequ-direction="mo"</small>
<av-media type="frequ" :media="media" line-color="darkorange"
frequ-direction="mo"
:frequ-lnum="60"/>
<small>AvMedia type="wfrom"</small>
<av-media type="wform" :media="media" line-color="blue"
:canv-width="200" :canv-height="40"/>
Expand Down
31 changes: 26 additions & 5 deletions src/components/AvMedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ const props = {
default: false
},

/**
* prop: 'frequ-direction'
* Direction for frequency visualization.
* lr - from left to right
* mo - from middle out
* lr when not recognized.
* Default: wform
*/
frequDirection: {
type: String,
default: 'lr'
},

/**
* prop: 'line-color'
* Line color.
Expand Down Expand Up @@ -239,19 +252,27 @@ const AvMedia = {
},

frequ: function (data) {
const c = this.frequLnum
const step = this.canvWidth / c
const middleOut = this.frequDirection === 'mo'
const start = middleOut ? this.canvWidth / 2 : 0
const c = middleOut ? this.frequLnum / 2 : this.frequLnum
const step = middleOut ? this.canvWidth / c / 2 : this.canvWidth / c
const h = this.canvHeight
const lw = this.lineWidth || 2
for (let i = 0; i < c; i++) {
const x = i * step + lw
const x = middleOut ? i * step : i * step + lw
const v = data.slice(x, x + step).reduce((sum, v) => sum + (v / 255.0 * h), 0) / step
const space = (h - v) / 2 + 2 // + 2 is space for caps
this.ctx.lineWidth = lw
this.ctx.lineCap = this.frequLineCap ? 'round' : 'butt'
this.ctx.moveTo(x, space)
this.ctx.lineTo(x, h - space)
this.ctx.moveTo(start + x, space)
this.ctx.lineTo(start + x, h - space)
this.ctx.stroke()

if (middleOut && i > 0) {
this.ctx.moveTo(start - x, space)
this.ctx.lineTo(start - x, h - space)
this.ctx.stroke()
}
}
},

Expand Down

0 comments on commit 7cb91a7

Please sign in to comment.