Application : Tone

Started by Stephane Fonteyne, January 20, 2010, 04:01:39 PM

Previous topic - Next topic

Stephane Fonteyne

Hi everyone,

I can be compiled my application without any problems.
When I try to run my program it's execute fine
I hear not a sound from my speaker.
Both program runs fine but it give not desired result
My question is why?
Can anyone say what the problem here?

Inpout32.inc

#If Not %DEF(%INPOUT32_INC)
   %INPOUT32_INC = 1

'
' Pull in Windows includes if necessary
'
#If Not %DEF(%WINAPI)
  #INCLUDE "win32api.inc"
#EndIf

'*==============================*/
'*            InpOut32.dll                               */
'*==============================*/
Declare Function Inp32 StdCall Lib "Inpout32.dll" Alias "Inp32"(PortAddress As Long) As Word
Declare Sub Out32 StdCall Lib "Inpout32.dll" Alias "Out32"(PortAddress As Long, byteData As Long)
   
#EndIf ' #IF NOT %DEF(%INPOUT32_INC)



Application 1 Tone1.bas
==================

Tone1.bas


'--------------------------------------------------------------------------------
Function FORM1_BUTTON1_BN_CLICKED ( _
                                  ControlIndex     As Long,  _  ' index in Control Array
                                  hWndForm         As Dword, _  ' handle of Form
                                  hWndControl      As Dword, _  ' handle of Control
                                  idButtonControl  As Long   _  ' identifier of button
                                  ) As Long
      '=====================================
      'VB6 source
      '
      'Private Sub Command1_Click()
      '  Dim n, t As Integer
      '
      '  For n = 1 To 100
      '    Out32 &h61, Inp32(&h61) Or 2
      '    For t = 1 To 10000 : Next t
      '    Out32 &h61, Inp32(&h61) And 253
      '    For t = 1 To 10000 : Next t
      '  Next n
      '=====================================
       
      ' Translate into PBWin
      Local n, t As Long
     
      For n = 1 To 1000
         Out32(&h61,(Inp32(&h61) Or 2))
         For t = 1 To 10000 : Next t
         Out32(&h61,(Inp32(&h61) And 253))
         For t = 1 To 10000 : Next t
      Next n
End Function


Application 2 : TestInpOut32.bas
========================



'--------------------------------------------------------------------------------
Function FORM1_BTNREADPORT_BN_CLICKED ( _
                                      ControlIndex     As Long,  _  ' index in Control Array
                                      hWndForm         As Dword, _  ' handle of Form
                                      hWndControl      As Dword, _  ' handle of Control
                                      idButtonControl  As Long   _  ' identifier of button
                                      ) As Long
     
     'Private Sub Command1_Click()
     '  Text2.Text = Str(Inp(Val("&H" + Text1.Text)))
     'End Sub
     
     Local value As String
     Local pAddr As Long
     
     value = FF_TextBox_GetText(HWND_FORM1_TEXTBOX1)
     pAddr = Inp32(Val(value))
     FF_TextBox_SetText(HWND_FORM1_TEXTBOX1, Hex$(pAddr))
     
End Function


'--------------------------------------------------------------------------------
Function FORM1_BTNWRITEPORT_BN_CLICKED ( _
                                       ControlIndex     As Long,  _  ' index in Control Array
                                       hWndForm         As Dword, _  ' handle of Form
                                       hWndControl      As Dword, _  ' handle of Control
                                       idButtonControl  As Long   _  ' identifier of button
                                       ) As Long

    'Private Sub Command2_Click()
    '  Out Val("&H" + Text1.Text), Val(Text2.Text)
    'End Sub
   
    Local sAddr, sData As String
   
    sAddr = FF_TextBox_GetText(HWND_FORM1_TEXTBOX1)
    sData = FF_TextBox_GetText(HWND_FORM1_TEXTBOX2)
    Out32(Val(sAddr & "0x"),Val(sData & "0x"))
    'Out32(Val("&h" & FF_TextBox_GetText(HWND_FORM1_TEXTBOX1)), Val(FF_TextBox_GetText(HWND_FORM1_TEXTBOX2)))

End Function



Thanks already to help me and the product is beginning to know better and who wants a project of the posts here book "PC ports on Windows"

Stephane

By the way: the files in attachtment : Tone1.zip, Tone2.zip, Tone3.zip, TestInpOut32.zip


Stephane Fonteyne
Ba. Elektronica - ICT
GSM : +32-xxxxxxxxxx
PowerBasic Developer & Hardware Support

Wilko Verweij

Hi,
I have no experience with reading from and writing to ports, but if you switch from VB to PB (and that's apparently what you do), you'd better not use empty for-next loops to suspend program execution 'cause PB is much faster. Use Sleep instead (does not even take CPU).
Wilko

Douglas McDonald

#2
I'm not sure what your trying to do or why you need to access any ports to generate a tone (frequency). There are many ways to do it but to generate a simple tone I just make a .wav in memory and use 'Playsound' from the windows WINMM.DLL (API)

This program generates the DTMF( Telephone touch tones) via the sound card that most every PC has.
#COMPILE EXE
'#DIM ALL



'PBWin7.02 and PB/CC1.0
'generate DTMF tones as a WAV and play them

DECLARE SUB DTMF(dial$)
DECLARE SUB Handshake()
DECLARE SUB Kissoff()

FUNCTION PBMAIN()
Handshake()
Kissoff()
dtmf("123418340103005F")  'dial the number 141 800 555 1212. Invalid characters are ignored

END FUNCTION



'####### the DTMF routines #########
'Extracted from WinAPI
%SND_MEMORY         = &H4         ' lpszSoundName points to a memory file
DECLARE FUNCTION PlaySound LIB "WINMM.DLL" ALIAS "PlaySoundA" (lpszName AS ASCIIZ, BYVAL hModule AS DWORD, BYVAL dwFlags AS DWORD) AS LONG

'define types
TYPE WAVEheader
                rID              AS STRING*4  ' Contains the characters "RIFF"
                rLen             AS LONG      ' The length of the data in the next chunk
                wID              AS STRING*4  ' Contains the characters "WAVE"
                fId              AS STRING*4  ' Contains the characters "fmt "
                fLen             AS LONG      ' Length of data in the format chunk
                wFormatTag       AS INTEGER   ' specifies the wave format, eg 1 = Pulse Code Modulation
                nChannels        AS INTEGER   ' Number of channels, 1=mono, 2=stereo
                nSamplesPerSec   AS LONG      ' Playback frequency
                nAvgBytesPerSec  AS LONG      ' Indicates the average number of bytes a second the data should be
                                              ' transferred at = nChannels * nSamplesPerSec * (nBitsPerSample / 8)
                nBlockAlign      AS INTEGER   ' Indicates the block alignment of the data in the data chunk. Software
                                              ' needs to process a multiplt of nBlockAlign at a time.
                                              ' nBlockAlign = nChannels * (nBitsPerSample / 8)
                wBitsPerSample   AS INTEGER   ' Format specific data area
                dId              AS STRING*4  ' Contains the characters "data"
                dLen             AS LONG      ' Length of data in the dData field
            END TYPE

TYPE onesecwav
     wv AS waveheader
     dta AS ASCIIZ *8000
END TYPE

GLOBAL w AS waveheader
GLOBAL ww AS onesecwav


SUB Handshake()

DIM p AS BYTE PTR ,q AS LONG PTR

duration&=100  '50ms per digit, spec says >40. Must not be >1000 unless buffer is made bigger.
scale#=2*3.14159265358979323#/8000
OldLetter$=" "
Samples&=duration&*8000/1000

                w.rID             = "RIFF"
                w.rLen            = 36+Samples&
                w.wID             = "WAVE"
                w.fId             = "fmt "
                w.fLen            = 16
                w.wFormatTag      = 1
                w.nChannels       = 1 'nChannels
                w.nSamplesPerSec  = 8000 'nSamplesPerSec
                w.nAvgBytesPerSec = 8000  '44100  '88200 'nChannels * nSamplesPerSec * (wBitsPerSample / 8)
                w.nBlockAlign     = 1 ' nChannels * (wBitsPerSample / 8)
                w.wBitsPerSample  = 8 'wBitsPerSample
                w.dId             = "data"
                w.dLen            = Samples&

ww.wv=w
p=VARPTR(ww.dta)
q=VARPTR(ww)
UDial$=UCASE$(dial$)

        freq1&=1400 'AND &h03)
        freq2&=1400' \ 4)
        'generate the tone samples
        FOR r& = 0 TO Samples&-1
              @p[r&]=50*(SIN(r&*scale#*freq1&) + SIN(r&*scale#*freq2&)) +128
        NEXT
        playsound BYVAL q,0 ,%SND_MEMORY ':REM play from memory,
        SLEEP 40   'ensure a gap between digits    =100ms on scope
        freq1&=2300 'AND &h03)
        freq2&=2300' \ 4)
        'generate the tone samples
        FOR r& = 0 TO Samples&-1
              @p[r&]=50*(SIN(r&*scale#*freq1&) + SIN(r&*scale#*freq2&)) +128
        NEXT
        playsound BYVAL q,0 ,%SND_MEMORY ':REM play from memory,


END SUB


SUB Kissoff()

DIM p AS BYTE PTR ,q AS LONG PTR

duration&=750  '50ms per digit, spec says >40. Must not be >1000 unless buffer is made bigger.
scale#=2*3.14159265358979323#/8000
OldLetter$=" "
Samples&=duration&*8000/1000

                w.rID             = "RIFF"
                w.rLen            = 36+Samples&
                w.wID             = "WAVE"
                w.fId             = "fmt "
                w.fLen            = 16
                w.wFormatTag      = 1
                w.nChannels       = 1 'nChannels
                w.nSamplesPerSec  = 8000 'nSamplesPerSec
                w.nAvgBytesPerSec = 8000  '44100  '88200 'nChannels * nSamplesPerSec * (wBitsPerSample / 8)
                w.nBlockAlign     = 1 ' nChannels * (wBitsPerSample / 8)
                w.wBitsPerSample  = 8 'wBitsPerSample
                w.dId             = "data"
                w.dLen            = Samples&

ww.wv=w
p=VARPTR(ww.dta)
q=VARPTR(ww)
UDial$=UCASE$(dial$)

        freq1&=1400 'AND &h03)
        freq2&=1400' \ 4)
        'generate the tone samples
        FOR r& = 0 TO Samples&-1
              @p[r&]=50*(SIN(r&*scale#*freq1&) + SIN(r&*scale#*freq2&)) +128
        NEXT
        playsound BYVAL q,0 ,%SND_MEMORY ':REM play from memory,
        SLEEP 100   'ensure a gap between digits    =100ms on scope


END SUB




SUB DTMF(dial$)
REM dial$ is the string to dial
DIM p AS BYTE PTR ,q AS LONG PTR
DIM LoBand(3) AS LONG, HiBand(3) AS LONG

duration&=50  '50ms per digit, spec says >40. Must not be >1000 unless buffer is made bigger.
scale#=2*3.14159265358979323#/8000
OldLetter$=" "

'specify the frequency pairs
LoBand(0)=697
LoBand(1)=770
LoBand(2)=852
LoBand(3)=941

HiBand(0)=1209
HiBand(1)=1336
HiBand(2)=1477
HiBand(3)=1633

code$="147*2580369#ABCD"
Samples&=duration&*8000/1000

                w.rID             = "RIFF"
                w.rLen            = 36+Samples&
                w.wID             = "WAVE"
                w.fId             = "fmt "
                w.fLen            = 16
                w.wFormatTag      = 1
                w.nChannels       = 1 'nChannels
                w.nSamplesPerSec  = 8000 'nSamplesPerSec
                w.nAvgBytesPerSec = 8000  '44100  '88200 'nChannels * nSamplesPerSec * (wBitsPerSample / 8)
                w.nBlockAlign     = 1 ' nChannels * (wBitsPerSample / 8)
                w.wBitsPerSample  = 8 'wBitsPerSample
                w.dId             = "data"
                w.dLen            = Samples&

ww.wv=w
p=VARPTR(ww.dta)
q=VARPTR(ww)
UDial$=UCASE$(dial$)

FOR digit& = 1 TO LEN(Udial$)
    index&=INSTR(code$,MID$(UCASE$(dial$),digit&,1))-1
    IF index&<>-1 THEN
        freq1&=LoBand(index& AND &h03)
        freq2&=HiBand(index& \ 4)
        'generate the tone samples
        FOR r& = 0 TO Samples&-1
             @p[r&]=50*(SIN(r&*scale#*freq1&) + SIN(r&*scale#*freq2&)) +128
        NEXT
        'add above code but don't add second freq
        playsound BYVAL q,0 ,%SND_MEMORY ':REM play from memory,
        SLEEP 17   'ensure a gap between digits   =50ms on scope
        END IF

        NEXT

END SUB


BTW some of this code I got off the PB web site but I don't remember the Author(S) for some of the snippets. Credits given.

If your trying to write to a port in XP-vista-win7 then you can't without a driver or mapping the port(not easy but there are utilities to do it). You have to get to level 0 in windows to talk directly to a port. (we are not talking com , LTP or USB port here but BUS ports)

If I'm in the ball park of what your looking for then I hope this helps. If not the I do not understand what your asking. Sorry

This is a better topic fro the PowerBasic forums since its not FF3 realated. Lot of info there.

Doug
Doug McDonald
KD5NWK
www.redforksoftware.com
Is that 1's and 0's or 0's and 1's?

Stephane Fonteyne

Hi Douglas,

Yes, you are on the right side. Can you tell me the many ways to do it but to generate a simple.

The aim here was to create a show as the remote user via the serial port for example a single button or schrollbar shifts then the tone changes

Can you send me an example in FF with that example

Thanks for your cooperation

Stephane Fonteyne
Ba. Elektronica - ICT
GSM : +32-xxxxxxxxxx
PowerBasic Developer & Hardware Support

Douglas McDonald

#4
Stephane,

QuoteYes, you are on the right side. Can you tell me the many ways to do it but to generate a simple.
I just told you in the post above. Please look at the code

Ok I understand the part that you want to change the tone with a slider or button click. I do not understand what you want to do over the serial port.

I'll help point you in the right direction but I will not write the code for you. The example I posted is very simple and to add a slider or button to change tones you should not need an example

Doug

If you just want to blink an LED you really don't want to do it via a serial port, you could but you'd need more hardware and I won't get into that. The LTP port would be better for that..........but there lies the problem of accessing the LTP port in Win2k, XP or above without a driver or mapping the port to allow access. To be very honest, you need to do is study up on how the ports work to get a better understanding of what your asking.

I know it seems so simple but in the end its not until you get the basics of how it all fits together. I've been doing it a long time and I still have to study. I'm new to FF3 and I have asked some pretty basic questions but not more than once and its syntax issues not concepts

I'm more than willing to spend my time to help as others have helped me. But.....YOU have to make an effort or no one will help. No one is going to write the code for you
Doug McDonald
KD5NWK
www.redforksoftware.com
Is that 1's and 0's or 0's and 1's?