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

Feat: Add channelIdx parameter to have the ability to create a region just for a specific channel #3234

Merged

Conversation

bdriguesdev
Copy link
Contributor

@bdriguesdev bdriguesdev commented Oct 5, 2023

Short description

Resolves #3220

Implementation details

Add channelIdx parameter to have the ability to create a region just for a specific channel

image

How to test it

You can use this code to test it:

code
import WaveSurfer from 'https://unpkg.com/wavesurfer.js@7/dist/wavesurfer.esm.js'
import RegionsPlugin from 'https://unpkg.com/wavesurfer.js@7/dist/plugins/regions.esm.js'

const wavesurfer = WaveSurfer.create({
  container: document.body,
  url: '/examples/audio/stereo.mp3',
  splitChannels: true,
})

// Initialize the Regions plugin
const wsRegions = wavesurfer.registerPlugin(RegionsPlugin.create())

wavesurfer.on('decode', () => {
  wsRegions.addRegion({
    start: 0,
    end: 8,
    content: 'Resize me',
    color: 'red',
    drag: false,
    resize: true,
    channelIdx: 1,
  })
  wsRegions.addRegion({
    start: 9,
    end: 39,
    content: 'Resize me',
    color: 'blue',
    drag: true,
    resize: true,
    channelIdx: 0,
  })
  wsRegions.addRegion({
    start: 100,
    end: 110,
    color: 'green',
    drag: true,
    resize: true,
  })
})

wavesurfer.on('interaction', () => {
  wavesurfer.play()
})

Screenshots

Checklist

  • This PR is covered by e2e tests
  • It introduces no breaking API changes

@bdriguesdev bdriguesdev force-pushed the feat/add-channel-id-option-to-regions branch from 224724c to 5a37baf Compare October 5, 2023 00:28
Copy link
Owner

@katspaugh katspaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the PR! Looks good overall, just left a couple comments. Cheers!

@@ -57,6 +58,8 @@ export type RegionParams = {
minLength?: number
/** Max length when resizing (in seconds) */
maxLength?: number
/** The id of the channel */
channelId?: number
Copy link
Owner

@katspaugh katspaugh Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In v6, this param was called channelIdx which was technically more correct because it's a channel's index, not an id.
Should we call it channelIndex maybe? Or just keep channelIdx for backwards compatibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you, for some reason I didn't realize it was an index instead of an id 🙈


constructor(params: RegionParams, private totalDuration: number) {
constructor(params: RegionParams, private totalDuration: number, wavesurfer: WaveSurfer) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally didn't pass the wavesurfer instance to the Region class to limit its responsibilities.
The only place where you're using it is to get the numberOfChannels, so I'd suggest to pass that instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it, thanks for explaining! I just pushed this change

@bdriguesdev bdriguesdev force-pushed the feat/add-channel-id-option-to-regions branch from 5a37baf to d1cae0a Compare October 11, 2023 00:40
@bdriguesdev bdriguesdev changed the title Feat: Add channelId parameter to have the ability to create a region just for a specific channel Feat: Add channelIdx parameter to have the ability to create a region just for a specific channel Oct 11, 2023
@katspaugh
Copy link
Owner

Thanks for addressing my comments!
Looks like the Regions tests are failing.

@bdriguesdev bdriguesdev force-pushed the feat/add-channel-id-option-to-regions branch from d1cae0a to c17d1cc Compare October 15, 2023 01:23
Copy link
Owner

@katspaugh katspaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@katspaugh katspaugh merged commit a29dd1c into katspaugh:main Oct 15, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regions with split channels
2 participants