Author Topic: Waveform and encrypted file...  (Read 104 times)

gvolti1970

  • Posts: 2
Waveform and encrypted file...
« on: 13 Jun '17 - 16:41 »
Hi everyone, I need some help to solve my problem

I have an application that record audio to ogg files...
these files are opened throught a customized player with button and a waveform...

Everything works very well...

Now the customer asked me to encrypt audio files...
so I decided to edit every single byte from encoder with a xor operation... --->  byte = byte xor 88
I have also changed player... works well...

The problem comes with waveform creation...
while first I passed the filename to waveform class, now I pass a decoding stream

result: If I don't generate the waveform, sound is reproduced well... if I generate the waveform, this is graphically perfect but sound is reproduced badly, as if it going at a higher speed...


opening file
Code: [Select]
If IO.File.Exists(_PlayFilename) = False Then preFileChannel = 0 : FileChannel = 0 : Exit Do

_fs = IO.File.Open(_PlayFilename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite)
preFileChannel = Un4seen.Bass.Bass.BASS_StreamCreateFileUser(Un4seen.Bass.BASSStreamSystem.STREAMFILE_NOBUFFER, BASSFlag.BASS_SAMPLE_FLOAT Or BASSFlag.BASS_STREAM_DECODE, _myStreamCreateUser, IntPtr.Zero)
FileChannel = AddOn.Fx.BassFx.BASS_FX_TempoCreate(preFileChannel, BASSFlag.BASS_SAMPLE_FLOAT)

        If FileChannel = 0 Then
            MessageBox.Show("Impossibile riprodurre il file !!", "Errore Apertura File", MessageBoxButtons.OK, MessageBoxIcon.Error)
            If preFileChannel <> 0 Then Bass.BASS_StreamFree(preFileChannel) : preFileChannel = 0
            Bass.BASS_Free()
        End If

        Bass.BASS_ChannelSetAttribute(FileChannel, BASSAttribute.BASS_ATTRIB_TEMPO_OPTION_USE_AA_FILTER, False)
        Bass.BASS_ChannelSetAttribute(FileChannel, BASSAttribute.BASS_ATTRIB_TEMPO, 0)

        SetEffects(FileChannel)
        GetWaveForm()

        Bass.BASS_ChannelPlay(FileChannel, Not Flag_PausePlaying)
        StatusPlayer = BASSActive.BASS_ACTIVE_PLAYING


generating Waveform
Code: [Select]
    Private Sub GetWaveForm(Optional ByVal ForceRendering As Boolean = False)

        If IO.File.Exists(PathLocale & "\cache\" & CType(gridRegistrazioniView.GetFocusedRow, rcdRegistrazioni).FileId.ToString & ".wf") = True AndAlso ForceRendering = False Then
            _WaveForm_File = New Misc.WaveForm()
            _WaveForm_File.FrameResolution = 0.005              '5ms
            _WaveForm_File.CallbackFrequency = 4000             '(4000*5ms=20sec)
            _WaveForm_File.ColorBackground = Color.FromArgb(255, 30, 30, 30)
            _WaveForm_File.ColorLeft = Color.LimeGreen
            _WaveForm_File.ColorLeftEnvelope = Color.Transparent
            _WaveForm_File.ColorRight = Color.Transparent
            _WaveForm_File.ColorRightEnvelope = Color.Transparent
            _WaveForm_File.DrawWaveForm = Misc.WaveForm.WAVEFORMDRAWTYPE.Mono
            _WaveForm_File.GainFactor = opt_WaveformGain

            _WaveForm_File.WaveFormLoadFromFile(PathLocale & "\cache\" & CType(gridRegistrazioniView.GetFocusedRow, rcdRegistrazioni).FileId.ToString & ".wf", True)

            If _WaveForm_File.FileName = "" Then

                Try
                    IO.File.Delete(PathLocale & "\cache\" & CType(gridRegistrazioniView.GetFocusedRow, rcdRegistrazioni).FileId.ToString & ".wf")

                Catch ex As Exception
                End Try

                GetWaveForm(True)

            Else
                _WaveForm_File.SyncPlayback(FileChannel)
                DrawWave()

            End If

        Else
            _WaveForm_File = New Misc.WaveForm()
            _WaveForm_File.NotifyHandler = New Misc.WAVEFORMPROC(AddressOf MyWaveFormCallback)
            _WaveForm_File.WinControl = Me
            _WaveForm_File.FrameResolution = 0.005              '5ms
            _WaveForm_File.CallbackFrequency = 4000             '(4000*5ms=20sec)
            _WaveForm_File.ColorBackground = Color.FromArgb(255, 30, 30, 30)
            _WaveForm_File.ColorLeft = Color.LimeGreen
            _WaveForm_File.ColorLeftEnvelope = Color.Transparent
            _WaveForm_File.ColorRight = Color.Transparent
            _WaveForm_File.ColorRightEnvelope = Color.Transparent
            _WaveForm_File.DrawWaveForm = Misc.WaveForm.WAVEFORMDRAWTYPE.Mono
            _WaveForm_File.GainFactor = opt_WaveformGain

            _WaveForm_File.RenderStart(preFileChannel, True)
            _WaveForm_File.SyncPlayback(FileChannel)
        End If

    End Sub

    Private Sub MyWaveFormCallback(ByVal framesDone As Integer, ByVal framesTotal As Integer, ByVal elapsedTime As TimeSpan, ByVal finished As Boolean)

        DrawWave()

    End Sub

    Private Sub DrawWave()

        If _WaveForm_File IsNot Nothing Then
            WaveFormBox.BackgroundImage = _WaveForm_File.CreateBitmap(WaveFormBox.Width, WaveFormBox.Height, -1, -1, opt_WaveformHQ)
        Else
            WaveFormBox.BackgroundImage = Nothing
        End If

    End Sub

    Private Sub DrawWavePosition(ByVal Position As Long, ByVal Length As Long)
        Dim X As Integer
        Dim BytePerPixel As Double
        Dim myBitmap As Bitmap = Nothing
        Dim myGraphic As Graphics = Nothing
        Dim myPen As Pen = Nothing

        If Length = 0 OrElse Position < 0 Then
            WaveFormBox.Image = Nothing
            Exit Sub
        End If

        Try
            BytePerPixel = Length / WaveFormBox.Width

            myPen = New Pen(Color.Red)
            myBitmap = New Bitmap(WaveFormBox.Width, WaveFormBox.Height)
            myGraphic = Graphics.FromImage(myBitmap)

            myGraphic.Clear(Color.White)
            X = Int(Position / BytePerPixel)
            myGraphic.DrawLine(myPen, X, 0, X, WaveFormBox.Height - 1)
            myBitmap.MakeTransparent(Color.White)

        Catch ex As Exception
            myBitmap = Nothing

        Finally
            If myPen IsNot Nothing Then myPen.Dispose()
            If myGraphic IsNot Nothing Then myGraphic.Dispose()
        End Try

        WaveFormBox.Image = myBitmap

    End Sub

    Private Sub WaveFormBox_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles WaveFormBox.MouseClick
        Dim Position As Long : Dim Length As Long
        Dim TotalTime As Double : Dim ElapsedTime As Double

        If _WaveForm_File Is Nothing Then Exit Sub

        If e.Button = Windows.Forms.MouseButtons.Left Then
            Position = _WaveForm_File.GetBytePositionFromX(e.X, WaveFormBox.Width, -1, -1)
            Bass.BASS_ChannelSetPosition(FileChannel, Position)

            If Bass.BASS_ChannelIsActive(FileChannel) = BASSActive.BASS_ACTIVE_STOPPED OrElse Bass.BASS_ChannelIsActive(FileChannel) = BASSActive.BASS_ACTIVE_PAUSED Then
                Length = Bass.BASS_ChannelGetLength(FileChannel)

                TotalTime = Bass.BASS_ChannelBytes2Seconds(FileChannel, Length)
                ElapsedTime = Bass.BASS_ChannelBytes2Seconds(FileChannel, Position)

                btnPlayerTempo.Text = Utils.FixTimespan(ElapsedTime, "HHMMSS") & " / " & Utils.FixTimespan(TotalTime, "HHMMSS")
                btnPlayerOrarioRelativo.Text = Convert.ToDateTime(gridRegistrazioniView.GetFocusedRowCellValue("OrarioInizio")).AddSeconds(ElapsedTime).ToLongTimeString

                Flag_PausePlaying = True : DrawWavePosition(Position, Length)
            End If

            DrawWave()
        End If

    End Sub

    Private Sub WaveFormBox_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles WaveFormBox.MouseDoubleClick
        Dim Position As Long

        If _WaveForm_File Is Nothing Then Exit Sub
        If Not (Bass.BASS_ChannelIsActive(FileChannel) = BASSActive.BASS_ACTIVE_STOPPED OrElse Bass.BASS_ChannelIsActive(FileChannel) = BASSActive.BASS_ACTIVE_PAUSED) Then Exit Sub

        If e.Button = Windows.Forms.MouseButtons.Left Then
            Position = _WaveForm_File.GetBytePositionFromX(e.X, WaveFormBox.Width, -1, -1)
            Bass.BASS_ChannelSetPosition(FileChannel, Position)
            Flag_PausePlaying = True : PlayFile()
            DrawWave()
        End If

    End Sub

    Private Sub WaveFormBox_BackgroundImageChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WaveFormBox.BackgroundImageChanged

        If WaveFormBox.BackgroundImage Is Nothing Then WaveFormBox.BackColor = Color.FromArgb(255, 30, 30, 30) Else WaveFormBox.BackColor = Color.Transparent

    End Sub

How can i Resolve this enigma???

Thanks a lot
GV

Ian @ un4seen

  • Administrator
  • Posts: 20063
Re: Waveform and encrypted file...
« Reply #1 on: 14 Jun '17 - 16:59 »
If I understand corrently, the problem you're having is that playback is too fast when you try to use the same decoding channel handle for both playback and the waveform generation? If so, it sounds like the waveform processing is taking data from the decoder, resulting in that data not being available for playback, hence the playback is skipping and sounds fast. I haven't used BASS.Net's WaveForm class myself (I'm not a .Net user), so I'm not familiar with what options it has, but the solution will probably be to use a separate decoding channel for that, or perhaps generate the waveform before starting playback?

gvolti1970

  • Posts: 2
Re: Waveform and encrypted file...
« Reply #2 on: 15 Jun '17 - 15:34 »
Hi Ian...

I've partly solved... the problem, as you said, was that I used a single decoding stream...
I solved opening file twice at the same time and assigning one for playback stream and the other for waveform stream...
everything work but it's very slow... I don't know if there is a best way to make it....

It would be nice if there was a callback delegate even for waveform rendering...

Thanks