How to change the volume of a PCM data stream (failed experiment)

solvable

My code was never used to process signed values, and as such bytes -> a short conversion did not correctly process the sign bit. This solved the problem correctly.

The question was ...

I am trying to change the amount of PCM data stream. I can extract single-channel data from a stereo file, make various silly experimental effects with samples, skipping / duplicating them / inserting zeros / etc, but I cannot find a way to change the actual values ​​of the sample in any way and get a reasonable output.

My attempts are very simple: http://i.imgur.com/FZ1BP.png

  • audio source data
  • values ​​- 10000
  • values ​​+ 10000
  • values ​​* 0.9
  • values ​​* 1.1

(value = value works fine - changes the wave, and it sounds the same)

The code for this is equally simple (I / O uses unsigned values ​​in the range 0-65535) <- , which was a problem, when reading correctly signed values, the problem was solved :

// NOTE: INVALID CODE int sample = ...read unsigned 16 bit value from a stream... sample -= 32768; sample = (int)(sample * 0.9f); sample += 32768; ...write unsigned 16 bit value to a stream... // NOTE: VALID CODE int sample = ...read *signed* 16 bit value from a stream... sample = (int)(sample * 0.9f); ...write 16 bit value to a stream... 

I'm trying to make the sample quieter. I would suggest that decreasing the amplitude (sample * 0.9) will lead to a more relaxed file, but as mentioned above 4 and 5. are clearly invalid. There is a similar question about SO , where MusiGenesis says that it got the correct results with code type sample = = 0.75 (yes, I experimented with values ​​other than 0.9 and 1.1).

The question is, am I doing something stupid or is this the whole idea of ​​multiplying by a constant error? I would like the end result to be something like this: http://i.imgur.com/qUL10.png

+8
audio pcm
source share
1 answer

Your fourth attempt is, of course, the right approach. Assuming your sample range is centered around 0, multiplying each sample by a different value is how you can change the volume or gain of the signal.

In this case, however, I would suggest that something funny happens behind the scenes when you multiply an int by a float and return an int. It's hard to say without knowing which language you are using, but this can be the cause of the problem.

+7
source share

All Articles