-
Oct 26th, 2019, 12:55 AM
#1
Thread Starter
New Member
Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
Hi, I am new here, currently I am working on a tone generator project, I have downloaded the following from somewhere, it is working on my laptop, but seems like the following is working on sample rate 44.1KHz, I would like to convert it to 192KHz, but there so many parameters, could somebody help me?
Code:
Private Declare Function GetMem8 Lib "msvbvm60" (ByRef src As Any, ByRef Dst As Any) As Long
Private Declare Function GetMem4 Lib "msvbvm60" (ByRef src As Any, ByRef Dst As Any) As Long
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundW" (ByRef pData As Any, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Declare Function Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) As Long
Private Const SND_MEMORY = &H4
Dim x As Single
Private Function PlayTone(ByVal fFrequency As Single, ByVal fDurationMS As Single) As Boolean
Dim bData() As Byte
Dim lSize As Long
Dim lSamples As Long
Dim lIndex As Long
Dim fPhase As Single
Dim fDelta As Single
lSamples = 44.1 * fDurationMS
lSize = lSamples + 44
fDelta = fFrequency / 44100 * 6.28318530717959
ReDim bData(lSize - 1)
GetMem4 &H46464952, bData(0): GetMem4 CLng(lSize - 8), bData(4)
GetMem8 233861439252950.2551@, bData(8)
GetMem8 28147927167.7968@, bData(16)
GetMem8 18940805779.77@, bData(24)
GetMem8 702234480110259.4049@, bData(32)
GetMem4 lSamples, bData(40)
For lIndex = 0 To lSamples - 1
bData(lIndex + 44) = Sin(fPhase) * 127 + 128
fPhase = fPhase + fDelta
If fPhase > 6.28318530717959 Then fPhase = fPhase - 6.28318530717959
Next
PlaySound bData(0), 0, SND_MEMORY
End Function
Sub Playlist()
PlayTone 30, 100
End Sub
Last edited by si_the_geek; Oct 26th, 2019 at 03:46 AM.
Reason: added Code tags
-
Oct 26th, 2019, 07:43 AM
#2
Junior Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
try
Code:
Private Function PlayTone(ByVal fFrequency As Single, ByVal fDurationMS As Single, SampleKHz As Single) As Boolean
Dim lRate As Long
Dim bData() As Byte
Dim lSize As Long
Dim lSamples As Long
Dim lIndex As Long
Dim fPhase As Single
Dim fDelta As Single
lRate = SampleKHz * 1000
lSamples = SampleKHz * fDurationMS
lSize = lSamples + 44
fDelta = fFrequency / (SampleKHz * 1000) * 6.28318530717959
ReDim bData(lSize - 1)
GetMem4 &H46464952, bData(0)
GetMem4 CLng(lSize - 8), bData(4)
GetMem8 233861439252950.2551@, bData(8)
GetMem8 28147927167.7968@, bData(16)
GetMem4 lRate, bData(24) 'SampleRate
GetMem4 lRate, bData(28) 'ByteRate = SampleRate * NumChannels * BitsPerSample/8
GetMem8 702234480110259.4049@, bData(32)
GetMem4 lSamples, bData(40)
For lIndex = 0 To lSamples - 1
bData(lIndex + 44) = Sin(fPhase) * 127 + 128
fPhase = fPhase + fDelta
If fPhase > 6.28318530717959 Then fPhase = fPhase - 6.28318530717959
Next
PlaySound bData(0), 0, SND_MEMORY
Debug.Print "Size: " & lSize
End Function
Sub Playlist()
PlayTone 30, 100, 44.1
End Sub
SampleRate=ByteRate as the code you posted has NumChannels=1 and BitsPerSample=8
-
Oct 26th, 2019, 09:39 AM
#3
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Oct 26th, 2019, 08:40 PM
#4
Junior Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
Ah, sorry for the confusion. I meant they numerically have the same value in this instance, not that they are the same thing.
-
Oct 26th, 2019, 10:24 PM
#5
Thread Starter
New Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
Thank you so much for your quite reply, ajsha. I am new to sound format. Your modified Sub Playlist()
Sub Playlist()
PlayTone 30, 100, 44.1
End Sub
Does it mean that if I want to play 30Hz, 100ms, 192KHz sampling Rate, I just modified as below:
Sub Playlist()
PlayTone 30, 100, 192
End Sub
if I need sample rate 192kHz/24 bits, how do I modify the code. "GetMem4 lRate, bData(28)".
-
Oct 27th, 2019, 06:44 AM
#6
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
There is no sample rate of 192
rtfm!
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Oct 27th, 2019, 01:32 PM
#7
Fanatic Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
That code sample is not meant to be easily edited or understood. It's using Currency literals to encode 64-Bit values. It does what it was intended to do. To understand what the numbers mean, if you have MSDN October 2001, look for a topic called "Reading Wave Files" in DirecX Visual Basic. It describes WAV file format, and some VB6 code to fill the structures. Online, check this article, which covers more additions to the format, but there is no VB code. There are probably easier resources to illustrate WAV file format.
-
Oct 27th, 2019, 08:38 PM
#8
Thread Starter
New Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
Thank you for the direction. I start with that first.
-
Oct 28th, 2019, 04:54 AM
#9
Junior Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
Yes "PlayTone 30, 100, 192" should give you a sample rate of 192kHz, however that code samples at 8bit. As qvb6 mentioned the original code uses currency values and these combine a number of parameters.
To show all parameters in the wave header you would need to add the function declaration
Code:
Private Declare Function GetMem2 Lib "msvbvm60" (ByRef src As Any, ByRef Dst As Any) As Long
and replace the block of parameter code with
Code:
'4 bytes ChunkID "RIFF" 52494646 (ascii values)
GetMem4 &H46464952, bData(0) '
'4 bytes ChunkSize
GetMem4 CLng(lSize - 8), bData(4)
'4 bytes Format "WAVE" 57415645 (ascii values)
GetMem4 &H45564157, bData(8)
'4 bytes Subchunk1ID "fmt " 666d7420
GetMem4 &H20746D66, bData(12)
'4 bytes Subchunk1Size 16 for PCM (&H10)
GetMem4 &H10&, bData(16)
'2 bytes AudioFormat PCM = 1
GetMem2 1, bData(20)
'2 bytes NumChannels Mono = 1, Stereo = 2, etc.
GetMem2 1, bData(22)
'4 bytes SampleRate 8000, 44100, etc.
GetMem4 lRate, bData(24)
'4 bytes ByteRate = SampleRate * NumChannels * BitsPerSample/8
GetMem4 lRate, bData(28)
'2 bytes BlockAlign = NumChannels * BitsPerSample/8
GetMem2 1, bData(32)
'2 bytes BitsPerSample 8 bits = 8, 16 bits = 16, etc.
GetMem2 8, bData(34)
'4 bytes Subchunk2ID "data" 64617461
GetMem4 &H61746164, bData(36)
'4 bytes Subchunk2Size = NumSamples * NumChannels * BitsPerSample/8
GetMem4 lSamples, bData(40)
Use the link posted by qvb6 for further info on wave headers and also check here. Try searching for "wave riff format" if you need further info.
-
Oct 28th, 2019, 05:10 AM
#10
Junior Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
btw "need to add the function declaration" isn't really true, its just that the GetMem2 name serves as a reminder to only copy 2 byte values to those memory locations as per the wave header specification.
-
Oct 28th, 2019, 08:50 AM
#11
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
Code:
Private Function PlayTone(ByVal fFrequency As Single, ByVal lSampleRate As Long, ByVal fDurationMS As Single) As Boolean
Dim bData() As Byte
Dim lSize As Long
Dim lSamples As Long
Dim lIndex As Long
Dim fPhase As Single
Dim fDelta As Single
Dim lSample As Long
lSamples = lSampleRate * fDurationMS / 1000
lSize = lSamples * 3 + 44
fDelta = fFrequency / lSampleRate * 6.28318530717959
ReDim bData(lSize - 1)
GetMem4 &H46464952, bData(0): GetMem4 CLng(lSize - 8), bData(4)
GetMem8 233861439252950.2551@, bData(8)
GetMem8 28147927167.7968@, bData(16)
GetMem4 lSampleRate, bData(24)
GetMem4 lSampleRate * 3, bData(28)
GetMem8 702234480110364.2627@, bData(32)
GetMem4 lSamples * 3, bData(40)
For lIndex = 0 To lSamples - 1
lSample = Sin(fPhase) * 8388607
bData(lIndex * 3 + 44) = lSample And &HFF
bData(lIndex * 3 + 45) = (lSample \ &H100) And &HFF
bData(lIndex * 3 + 46) = (lSample \ &H10000) And &HFF
fPhase = fPhase + fDelta
If fPhase > 6.28318530717959 Then fPhase = fPhase - 6.28318530717959
Next
PlaySound bData(0), 0, SND_MEMORY
End Function
-
Oct 30th, 2019, 12:32 AM
#12
Thread Starter
New Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
The trick and ajsha, thanks again for the help. I'm doing a lot of reading but still can't understand fully.
The trick, I have just tried your code and do the following:
PlayTone 39.813, 192, 1000
PlayTone 40.153, 192, 1000
PlayTone 41.255, 192, 1000
PlayTone 42.251, 192, 1000
I found that in between the played frequencies, there is this noisy or cracking sound, is it from my speaker or the code itself? Any chance to reduce the noise?
-
Oct 30th, 2019, 03:27 AM
#13
Thread Starter
New Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
Ajsha, big thank you for you code. I have key in second portion of your code, I would like to check with you, whether the GetMem2, GetMem4 and GetMem8 are interchangeable:
'4 bytes ChunkID "RIFF" 52494646 (ascii values)
GetMem4 &H46464952, bData(0) '
'4 bytes ChunkSize
GetMem4 CLng(lSize - 8), bData(4)
'4 bytes Format "WAVE" 57415645 (ascii values)
GetMem4 &H45564157, bData(8)
- "why the original is GetMem8, bData(8)
'4 bytes Subchunk1ID "fmt " 666d7420
GetMem4 &H20746D66, bData(12)
'4 bytes Subchunk1Size 16 for PCM (&H10)
GetMem4 &H10&, bData(16)
- "why the original is GetMem8, bData(16)
'2 bytes AudioFormat PCM = 1
GetMem2 1, bData(20)
'2 bytes NumChannels Mono = 1, Stereo = 2, etc.
GetMem2 1, bData(22)
'4 bytes SampleRate 8000, 44100, etc.
GetMem4 lRate, bData(24)
- "why the original is GetMem8, bData(24)
'4 bytes ByteRate = SampleRate * NumChannels * BitsPerSample/8
GetMem4 lRate, bData(28)
- "why the original is GetMem8, bData(28)
'2 bytes BlockAlign = NumChannels * BitsPerSample/8
GetMem2 1, bData(32)
- "why the original is GetMem8, bData(32)
'2 bytes BitsPerSample 8 bits = 8, 16 bits = 16, etc.
GetMem2 8, bData(34)
'4 bytes Subchunk2ID "data" 64617461
GetMem4 &H61746164, bData(36)
'4 bytes Subchunk2Size = NumSamples * NumChannels * BitsPerSample/8
GetMem4 lSamples, bData(40)
- "why the original is GetMem8, bData(40)
-
Oct 30th, 2019, 11:32 AM
#14
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
Originally Posted by Derek Sim
The trick and ajsha, thanks again for the help. I'm doing a lot of reading but still can't understand fully.
The trick, I have just tried your code and do the following:
PlayTone 39.813, 192, 1000
PlayTone 40.153, 192, 1000
PlayTone 41.255, 192, 1000
PlayTone 42.251, 192, 1000
I found that in between the played frequencies, there is this noisy or cracking sound, is it from my speaker or the code itself? Any chance to reduce the noise?
Hmm..
You wanted 192 KHz but why you do use 192 Hz instead? The proper value is 192000.
Regarding the clicks just what did you expect if you set the length of the sound which isn't multiple of the wave length? Just see what happens if you use 39.813 HZ with the 1 sec audio:
-
Oct 30th, 2019, 10:15 PM
#15
Thread Starter
New Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
Thank you The trick for pointing out, I taught 192 was multiple with 1000 inside the code.
Here I tried the corrected code as follows:
PlayTone 39.813, 192000, 1004.696958279960
PlayTone 40.153, 192000, 1021.094314248000
PlayTone 41.255, 192000, 1018.058417161560
PlayTone 42.251, 192000, 1017.727391067670
formula =Roundup(Dur/(1/Hz)) * (1/Hz)
Is it correct?
-
Oct 31st, 2019, 05:53 AM
#16
Junior Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
The parameters for my code and the tricks code are different (kHz vs Hz) so use 192 for my function and 192000 for the trick's.
All of the names GetMem2, GetMem4, GetMem4 are just aliases for the same function (all do _exactly_ the same thing). They all write the amount of data given in the function's first parameter to the location specified in the second parameter. The reason to use different names is just as a reminder of how many bytes should be presented in the first parameter. eg GetMem4 &H10, bData(16) will write 2 bytes, so the GetMem'4' is a reminder to specify a long value (&H10&) as the first parameter. This is to correspond with the layout of the wave header format as specified in the link posted earlier.
In the end we are just trying to write the values of frequency, bitrate etc into the correct locations in the 44 byte header. The code I posted separates out the different parameters to be written to the header so its easier to see what is going on and its easier to change the values. The original code you posted combines groups of these values into larger 8 byte blocks.
eg the original code has the currency value 702234480110259.4049. This translates in hex to 0x6174616400080001 which is 3 values combined (61746164 - 0008 - 0001) which are the 4 byte string value "data", the 2 byte value of bits per sample (8) and the 2 byte value of block alignment value (1).
-
Nov 3rd, 2019, 10:10 PM
#17
Thread Starter
New Member
Re: Signal Generator - How to change the Sample Rate from 44.1KHz to 192KHz
Originally Posted by ajsha
The parameters for my code and the tricks code are different (kHz vs Hz) so use 192 for my function and 192000 for the trick's.
All of the names GetMem2, GetMem4, GetMem4 are just aliases for the same function (all do _exactly_ the same thing). They all write the amount of data given in the function's first parameter to the location specified in the second parameter. The reason to use different names is just as a reminder of how many bytes should be presented in the first parameter. eg GetMem4 &H10, bData(16) will write 2 bytes, so the GetMem'4' is a reminder to specify a long value (&H10&) as the first parameter. This is to correspond with the layout of the wave header format as specified in the link posted earlier.
In the end we are just trying to write the values of frequency, bitrate etc into the correct locations in the 44 byte header. The code I posted separates out the different parameters to be written to the header so its easier to see what is going on and its easier to change the values. The original code you posted combines groups of these values into larger 8 byte blocks.
eg the original code has the currency value 702234480110259.4049. This translates in hex to 0x6174616400080001 which is 3 values combined (61746164 - 0008 - 0001) which are the 4 byte string value "data", the 2 byte value of bits per sample (8) and the 2 byte value of block alignment value (1).
Thank you Ajsha. I am beginning to understand ....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|