Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Memblock Sounds Help?

Author
Message
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 10th Jul 2012 01:05 Edited at: 10th Jul 2012 01:07
I'm making a lock-picking game and I want to generate some sounds for it. I've tried but I don't understand how sound works so it's been trial and error.
Also the first time I play the sound it sounds different to subsequent plays: why is this?



I want clicking sounds for when the pins pop into place, and another sound for when the lock is successfully picked.

Shh... you're pretty.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 10th Jul 2012 01:59
Here's an EXE of a newer version with loaded in sound -->
Have fun!

Shh... you're pretty.

Attachments

Login to view attachments
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 10th Jul 2012 04:26
Wow, you're pushing this challenge to the limit Obese! I'll look forward to seeing what you can come up with.

I've never played around with it, but I'm going to assume that each sample value describes the current amplitude of the wave. Just looking at your make_sound subroutine, you appear to be addressing the memory bitwise when really it should be bytewise, so I changed this:
to this:


I don't have a DBC compiler at my hands, but this should sound like a sine-wave. Does it?



TheComet

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 10th Jul 2012 05:08 Edited at: 10th Jul 2012 05:23
posting accident....

Enjoy your day.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 10th Jul 2012 05:21
@OBese
The sound header is defined by:
1. bitspersample : can be 8 or 16 only for DBC
2. samplerate : listed in help as frequency, but this is confusing for it is not pitch, but the number of samples that can pass by in 1 second. Common samplerates are 96000, 48000, 44100, 22050, 11025, 8000
3. numberofsamples : equal to samplerate*(bitspersample/8)*(length in seconds)

A sample is the smallest part of a digital sound. As TheComet assumed, it an amplitude at any given point in the sample stream. The cycles of the amplitudes determine the pitch. The change in amplitudes determines the timbre. In DBCs case, the sample will either be 1 byte for 8 bit sampling or 2 bytes for 16 bit sampling.

So, don't try and use 'frequency' (meaning sample rate) to set the pitch of your sound. This will actually set the resolution or clarity of the sound (combined with bitspersample). You can control pitch by calculating the cycles over time or once you have your sound use SET SOUND SPEED to alter the sample rate of the finished product.

Enjoy your day.
Fluffy Rabbit
User Banned
Posted: 14th Jul 2012 01:47
I once tried to make a music player using generated sounds. The problem was that my generated square wave sounds (made with a for-next loop that skipped over a certain number of spaces based on frequency) produced sound output that was incredibly quiet.

I recently settled on just changing the pitch of a loaded WAV file, but I do wish that I could generate square, sine, and fading sounds at an audible amplitude using code alone.

Even just being able to generate a square wave would be great. Is there a specific way I can do this, Latch? Is there a specific way to make an audible low-resolution sound?
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 14th Jul 2012 11:05 Edited at: 14th Jul 2012 11:20
Quote: "Even just being able to generate a square wave would be great. Is there a specific way I can do this, Latch? Is there a specific way to make an audible low-resolution sound? "

Yeah, there's a few examples over the years that I've done in the DBC Challenges with noise wave froms specifically. Rain, and lightning and such. I think there's like a sliding stone sound in a rubiks cube demo I did.

The thing to keep in mind when making the audio is the amplitude for 8 bit must range between 0 and 255, and for 16 bit -32767 and 32768. Since 16 bit uses a signed WORD value, you have to create an algorithm for 2's compliment in order to write the correct WORD value to a memblock. I'm not at a computer with DBC so I cant test this but a sine waveform tuned to A 440 playing for 1 sec would look like



Enjoy your day.
Fluffy Rabbit
User Banned
Posted: 14th Jul 2012 22:02
@Latch-

It is amazing what you can do with zero testing.

I hear an audible sound, but it sounds more like a low trumpet. When I set it to 100 hz, I hear a higher sound.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 15th Jul 2012 01:32 Edited at: 15th Jul 2012 02:16
Quote: "I hear an audible sound, but it sounds more like a low trumpet"

Ooops! I got the cycles calculation flip-flopped:

anginc#=(samppercycle+.)/360.0

should be

anginc#=360.0/(samppercycle+.)

And the trumpet like sound is because the algorithm was more like a saw-tooth waveform - I forgot to adjust the 8 bit values to oscillate from -128 to 127 and converted to 0 to 255. Here's the adjustment for sin:



Enjoy your day.
Fluffy Rabbit
User Banned
Posted: 15th Jul 2012 03:05
@Latch-

It worked that time! Quite a beautiful tone, just like what Audacity can generate.

I know I probably sound like an idiot for saying this, but is there a way to get a square waveform? It would be cool to make those loud retro sound effects.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 15th Jul 2012 04:54 Edited at: 15th Jul 2012 04:56
Same basic code, but we only want values at the top, or bottom of the wave. For 8 bit sound that's values 127 and 128. That changes the algorithm slightly.




You can control the pulse width of the square waveform by changing the trigger values for the variable sign#. For example

sign# = 127*sin(ang#)

if sign# <= 50 then b=128
if sign# > 50 then b=127

This would make the pulse wider on the bottom part of the wave and narrower on the part making it a little thinner or tinny. When the top and bottom are equal (square) when sign# is at zero, that's when the sound is fatter.

If you layer the sounds and slightly detune them, you can get that fat analog sound.



Enjoy your day.
Fluffy Rabbit
User Banned
Posted: 17th Jul 2012 04:45
@Latch-

Awesome! The first sound was exactly what I wanted. I knew there was a science to it, I just couldn't figure it out.

The second sound was cool too, sounded a bit like Tron.
Fluffy Rabbit
User Banned
Posted: 21st Jul 2012 23:56 Edited at: 9th Aug 2012 07:26
Well, this little thing took some work. I started working on a music engine similar to what you would find in QBASIC a couple years ago, but only recently have I fixed the pitches to more closely match the European musical scale and applied the correct wave generation formula. Thanks, Latch!

Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd Jul 2012 05:13
That's pretty impressive and nostalgic.
However, when I tried to make my own song it sounded awful! I don't understand the way you've set out the notes it doesn't seem to match the usual sharps and flats. Maybe I'm just doing something wrong. But for example I found this plays a major C scale:

sg(1,1,4,"cc dd ee ff gg 3aa 3bb 3cc ")

but why does it jump from 1 to 3, shouldn't they be 2's?

It's a bit late for me to look into the code but I will have another go tomorrow.

Shh... you're pretty.
Fluffy Rabbit
User Banned
Posted: 26th Jul 2012 23:25 Edited at: 9th Aug 2012 07:27
@TheComet-

Octave 2 is actually octave 1. By default, it starts on octave 2 (1). Octave 3 is actually octave 2. Octave 4 is actually octave 3. Past octave 5, which is actually octave 4, the notes sound distorted and imprecise.

Here's a little program I wrote to randomly generate music:

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 29th Jul 2012 06:49
Is the format in DBC different than DBP? I know there's plenty of stuff already for DBP, but I don't remember if I didn't anything in DBC with memblock sounds.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 1st Aug 2012 22:57
Quote: "Is the format in DBC different than DBP? "

I'm not positive but I think DBPro has a 28 byte header and DBC has a 12 byte header. I know that DBC only converts between a DB sound and memblock as monophonic. So if you convert a stereo DB sound to a memblock, it'll only be half of the song. And since there is no stereo indicator in the header of a DBC memblock sound, then I assume the conversion from memblock to sound would also be monophonic. Perhaps DBPro can handle stereo conversions. So I would guess there are some differences.

Enjoy your day.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 2nd Aug 2012 15:32
Quote: "It is amazing what you can do with zero testing."


What is zero testing? Google didn't help in that regard.

TheComet


Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 2nd Aug 2012 23:14
Quote: "What is zero testing?"

Just an expression meaning "without any testing" in this case.

Enjoy your day.

Login to post a reply

Server time is: 2024-04-18 16:54:57
Your offset time is: 2024-04-18 16:54:57