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
1

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
7

Complete !

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

Cart #dusaruboye-0 | 2019-10-06 | Code ▽ | Embed ▽ | No License
7

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
4

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 you have. :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 2022-06-17 16:04)
4

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 | 2019-10-06 | Code ▽ | Embed ▽ | No License
4

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

@dw817
Hi, I wonder how to change volume of BGM and found this entry. Your code works well. Thank you!
I referred your code and found some APIs(band, bor, shr, and shl) are deprecated now. I rewrote as the following:

-- return masked bits
-- _val  target value
-- _mask contains 0-7 bit mask
-- _shr  for shift right
function bitget(_val,_mask,_shr)
 local result=0
 for i=1,#_mask do
  _bit_pos=sub(_mask,i,i)
  result+=(_val&(1<<_bit_pos))>>_shr
 end
 return result
end

-- modify masked bits
-- _val  target value
-- _mod  modify value to be set
-- _mask conrains 0-7 bit mask
-- _shl  for shift left
function bitmod(_val,_mod,_mask,_shl)
 return bitclr(_val,_mask)|(_mod<<_shl)
end

-- clear masked bits
-- _val  target value
-- _mask contains 0-7 bit mask
function bitclr(_val,_mask)
 local result=_val
 for i=1,#_mask do
  _bit_pos=sub(_mask,i,i)
  result-=result&(1<<_bit_pos)
 end
 return result
end
P#113251 2022-06-17 06:34 ( Edited 2022-06-17 06:35)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 08:54:28 | 0.034s | Q:34