BASS.NET API for the Un4seen BASS Audio Library

WaveFormGetBytePositionFromX Method

BASS.NET API for the Un4seen BASS Audio Library
Translates a X coordinate of a drawn WaveForm graphic to the related byte position within the rendered stream.

Namespace:  Un4seen.Bass.Misc
Assembly:  Bass.Net (in Bass.Net.dll) Version: 2.4.17.5
Syntax

public long GetBytePositionFromX(
	int x,
	int graphicsWidth,
	int frameStart,
	int frameEnd
)

Parameters

x
Type: SystemInt32
The X coordinate from which to get the byte position (between 0 and graphicsWidth-1).
graphicsWidth
Type: SystemInt32
The total width of the drawn WaveForm graphic (e.g. use pictureBox.Width).
frameStart
Type: SystemInt32
The rendered frame which was used to start creating the image (specify 0 or -1 if the start was from the beginning).
frameEnd
Type: SystemInt32
The rendered frame which was used to end creating the image (specify -1 for the end of the WaveForm).

Return Value

Type: Int64
The converted byte position of the stream which relates to the given X coordinate, else -1.
Remarks

This method might be used to translate a graphics coordinate to a byte position within a stream. The return value might for example be used by BASS_ChannelSetPosition(Int32, Int64, BASSMode) to set a new playback position.

Note: The byte position relates to the resolution you used with RenderStart(Int32, Boolean)! So if you for example used the BASS_SAMPLE_FLOAT with RenderStart(Int32, Boolean) the byte position relates to 32-bit sample data. However, if your playing stream uses only a 16-bit resolution (e.g. you used BASS_DEFAULT with BASS_StreamCreateFile(String, Int64, Int64, BASSFlag)), the returned byte position will not match! So make sure when you are calling BASS_ChannelSetPosition(Int32, Int64, BASSMode) with this return value, that your stream resolution is the same as the resolution used with RenderStart(Int32, Boolean). Otherwise you must convert the returned byte position (e.g. from 32-bit to 16-bit: pos = returnvalue/2). Or for ease of use you might use the SyncPlayback(Int32) method to ensure, that the return value of this method will already be converted accordingly for you!

Examples

Using a pictureBox and the MouseDown event handler:
// assuming the WaveForm is fully displayed in the pictureBox...
private void DrawWave()
{
  if (WF != null)
    this.pictureBox1.BackgroundImage = WF.CreateBitmap(this.pictureBox1.Width, 
                                                       this.pictureBox1.Height, 
                                                       -1, -1, false);
  else
    this.pictureBox1.BackgroundImage = null;
}

private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
  if (WF == null)
    return;
  long pos = WF.GetBytePositionFromX(e.X, this.pictureBox1.Width, -1, -1);
  Console.WriteLine("Position = {0}, Seconds = {1}", pos, Bass.BASS_ChannelBytes2Seconds(_stream, pos));
  Bass.BASS_ChannelSetPosition(_stream, pos);
}
See Also

Reference