Log In  

All,

I'm looking for a way to control SFX volume from within the code.

For example, as my main character approaches an audio source, it should become louder.

Is this possible?

Thanks in advance for any leads!

P#68524 2019-10-05 16:44

It's not directly possible. Two approaches to fake it that I can see:

1) have multiple SFX for one sound, each representing a different "loudness", and then pick depending on distance
2) (hacky and more advanced, but more powerful) use direct memory accesses to rewrite SFX on the fly - see https://www.lexaloffle.com/bbs/?tid=2341

P#68543 2019-10-05 21:31

Thank you @krajzeg.

Yeah, #1 was what I assumed I'd have to do.

I will study up on #2 though.

Thanks again!

P#68553 2019-10-06 01:08
:: dw817

Lazzoak, I'm working on it. Halfway there. Should be done by tomorrow evening.

  print("volume="..volume)
  if (btn(0)) volume-=1
  if (btn(1)) volume+=1
  volume=mid(-8,volume,8)
  reload(12800,12800,affectfx*68)
  for i=0,affectfx-1 do

If you can do it before me though, that's fine. :)

P#68555 2019-10-06 01:50
:: dw817
3

Complete !

Anything higher than 0 increases volume on all the other stuff.

Cart [#dusaruboye-0#] | Code | 2019-10-06 | No License | Embed
3

Pretty sure the start of this code can lead to all kinds of interesting things like change pitch, have percussion only, and change tempo. And of course it could be optimized tons. :)

HOPE THIS HELPS !

P#68556 2019-10-06 02:06 ( Edited 2019-10-06 02:30)

Wow, thank you @dw817

I'm studying this as we speak. I'm still somewhat new to pico 8 so working directly with memory is unfamiliar territory. But seems time I learn, so this gives me something to dissect.

P#68557 2019-10-06 06:09

@dw817 seriously?

 bit={}
 for i=7,0,-1 do
  if n>=2^i then
   bit[i]=1
   n-=2^i
  else
   bit[i]=0
  end
 end
 a=bit[1]+bit[2]*2+bit[3]*4

if you are not confortable with bit math, don't offer "helper" functions to beginners.

@lazzoak as per @krajzeg link, use bit masking to read/write sound volume.

-- chart sfx volume
cls()
fillp(0x0f0f)
rectfill(0,64,32,64-8,0x1)
fillp()

-- change to chart another sfx bank
local sfx_id=0
-- starting address
local mem=0x3200+68*sfx_id

for i=0,31 do
 -- read note
 local s=peek2(mem)
 -- bits 9-11 rebased to [0,7] range
 local vol=shr(band(0x0e00,s),9)
 -- print curve
 pset(i,64-vol,5+vol)
 mem+=2
end
P#68561 2019-10-06 08:29
:: dw817

I just wanted to get it done since no-one had ever provided working P8 source-code to date to change volume - and it works. And 3-stars. Woo hoo ! Someone must've liked it !

Wow, freds72. What an attitude. :D

Haven't your parents ever told you if you don't have anything nice to say, don't say anything at all ? Have I ever said something like what you did to you or anyone else ? Ever ? Don't think so.

Despite your dour attitude, you provided optimization which is the correct SHR. And I thank you for that. I knew it was SHL or SHR, I just have trouble with those. I'll sit down with that later and make a bit-function to help me (mostly) and perhaps others in the future.

As for you lazzoak, glad to help ! Writing that code was a learning experience for me too.

Do you understand the SHR command ? It's more complex than simpler exponent. Here is some information on it:

https://en.wikipedia.org/wiki/Logical_shift

P#68572 2019-10-06 14:38 ( Edited 2019-10-06 15:06)
:: dw817
1

Ah ! Just wrote up a new bit library and quashed (is that a word ?) the massive code for early bit-handling.

New functions are:
+ bitget() .. get number w string chars 0-7
+ bitmod() .. modify number w string chars 0-7
+ bitset() .. set bits in number from chars 0-7
+ bitclr() .. clear bits in number from chars 0-7
+ bittest().. returns true or false on bit set

Which leaves only this to read and write volume now:

n=peek(12800+i*68+j)
p=bitget(n,"123",1)
p+=volume
p=mid(0,p,7)
n=bitmod(n,p,"123",1)
poke(12800+i*68+j,n)

Here is new cart:

Cart [#mogupewaki-0#] | Code | 2019-10-06 | No License | Embed
1

P#68584 2019-10-06 17:47 ( Edited 2019-10-06 17:49)

[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-11-15 04:10 | 0.056s | 2097k | Q:54