dmix plugin
Quoted 8 lines
Quoted 2 lines
b) sum overflow: we can lower volume of samples before sum; I think that hardware works in this way, too
Here I don't understand you. Suppose we have 3 samples to mix:
a =0x7500
b =0x7400
c =0x8300
Quoted 7 lines
If you do a + b + c (in this order) you obtain:
d=0
d += a -> 7500
d += b ->0xe900->0x7fff
d += c ->0x02ffwhile the correct result is
0x6c00. You see?
AFAIK most hardware does not mix by reducing volume before the sum. On the contrary, it is usually summed "as is" to a wider register, and often even so used. For example, a sound card able to mix 16 chanels of 16 bits would have a 16+4 bits or 24 bit register were the channels are added and no saturation can occur. In good hardware this would not even be downscaled back to 16 bits, but a 24 bit D/A converter would be used instead. In older times (Gravis Ultra Sound and I think older SB AWE) this could easily be spotted by the difference in supported "hardware" channels and "software" channels. A card with a 32 bit sum register and 24 bit DA could support (as above) 16 hardware channels and for example 64 software channels (mixed together in quadrouplets to the 16 hw).
In our case, such "solution" would have to affect the whole buffer, meaning we would need 3 (or better yet 4) bytes per sample, which would eventually get reduced back to 2 bytes on the way out to the sound card. This seems neither elegant nor memory efficient but would work, and also solves the "a)" problem because we don't need to saturate so an atomic add can be performed on each sample.
--------------
Fycio (J.Sobierski)
[email protected]
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Quoted 27 lines · Jaroslaw Sobierski
Quoted 8 lines
Quoted 2 lines
b) sum overflow: we can lower volume of samples before sum; I think that hardware works in this way, too
Here I don't understand you. Suppose we have 3 samples to mix:
a =0x7500
b =0x7400
c =0x8300Quoted 7 lines
If you do a + b + c (in this order) you obtain:
d=0
d += a -> 7500
d += b ->0xe900->0x7fff
d += c ->0x02ffwhile the correct result is
0x6c00. You see?AFAIK most hardware does not mix by reducing volume before the sum. On the contrary, it is usually summed "as is" to a wider register, and often even so used. For example, a sound card able to mix 16 chanels of 16 bits would have a 16+4 bits or 24 bit register were the channels are added and no saturation can occur. In good hardware this would not even be downscaled back to 16 bits, but a 24 bit D/A converter would be used instead. In older times (Gravis Ultra Sound and I think older SB AWE) this could easily be spotted by the difference in supported "hardware" channels and "software" channels. A card with a 32 bit sum register and 24 bit DA could support (as above) 16 hardware channels and for example 64 software channels (mixed together in quadrouplets to the 16 hw).
Quoted 6 lines
In our case, such "solution" would have to affect the whole buffer, meaning we would need 3 (or better yet 4) bytes per sample, which would eventually get reduced back to 2 bytes on the way out to the sound card. This seems neither elegant nor memory efficient but would work, and also solves the "a)" problem because we don't need to saturate so an atomic add can be performed on each sample.
Yes, this solution is good. I've though about it, too. Unfortunately, it adds additional transfers including saturation from the "sum" ring buffer to the DMA buffer of hardware.
Jaroslav
-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Quoted 37 lines · Jaroslav Kysela
Quoted 27 lines · Jaroslaw Sobierski
Quoted 8 lines
Quoted 2 lines
b) sum overflow: we can lower volume of samples before sum; I think that hardware works in this way, too
Here I don't understand you. Suppose we have 3 samples to mix:
a =0x7500
b =0x7400
c =0x8300Quoted 7 lines
If you do a + b + c (in this order) you obtain:
d=0
d += a -> 7500
d += b ->0xe900->0x7fff
d += c ->0x02ffwhile the correct result is
0x6c00. You see?AFAIK most hardware does not mix by reducing volume before the sum. On the contrary, it is usually summed "as is" to a wider register, and often even so used. For example, a sound card able to mix 16 chanels of 16 bits would have a 16+4 bits or 24 bit register were the channels are added and no saturation can occur. In good hardware this would not even be downscaled back to 16 bits, but a 24 bit D/A converter would be used instead. In older times (Gravis Ultra Sound and I think older SB AWE) this could easily be spotted by the difference in supported "hardware" channels and "software" channels. A card with a 32 bit sum register and 24 bit DA could support (as above) 16 hardware channels and for example 64 software channels (mixed together in quadrouplets to the 16 hw).
Quoted 6 lines
In our case, such "solution" would have to affect the whole buffer, meaning we would need 3 (or better yet 4) bytes per sample, which would eventually get reduced back to 2 bytes on the way out to the sound card. This seems neither elegant nor memory efficient but would work, and also solves the "a)" problem because we don't need to saturate so an atomic add can be performed on each sample.
Quoted 3 lines
Yes, this solution is good. I've though about it, too. Unfortunately, it adds additional transfers including saturation from the "sum" ring buffer to the DMA buffer of hardware.
I remember you that the main point of dmix existence is the "direct" part.
If we'd need to use an intermediate buffer and a mixing thread, the dmix approach lose our interest.
A solution might be to have a shared parallel sw ring buffer where to store the exact value:
xadd(sw, *src);
do {
v = *sw;
if (v > 0x7fff)
s = 0x7fff;
else if (v < -0x8000)
s = -0x8000;
else
s = v;
*hw = v;
} while (unlikely(v != *sw));This should solve also the atomicity update.
Comments?
Signature · 11 lines
Abramo Bagnara mailto:[email protected] Opera Unica Phone: +39.546.656023 Via Emilia Interna, 140 48014 Castel Bolognese (RA) - Italy ------------------------------------------------------- This sf.net email is sponsored by:ThinkGeek Welcome to geek heaven. http://thinkgeek.com/sf
Quoted 63 lines · Abramo Bagnara
Quoted 37 lines · Jaroslav Kysela
Quoted 27 lines · Jaroslaw Sobierski
Quoted 8 lines
Quoted 2 lines
b) sum overflow: we can lower volume of samples before sum; I think that hardware works in this way, too
Here I don't understand you. Suppose we have 3 samples to mix:
a =0x7500
b =0x7400
c =0x8300Quoted 7 lines
If you do a + b + c (in this order) you obtain:
d=0
d += a -> 7500
d += b ->0xe900->0x7fff
d += c ->0x02ffwhile the correct result is
0x6c00. You see?AFAIK most hardware does not mix by reducing volume before the sum. On the contrary, it is usually summed "as is" to a wider register, and often even so used. For example, a sound card able to mix 16 chanels of 16 bits would have a 16+4 bits or 24 bit register were the channels are added and no saturation can occur. In good hardware this would not even be downscaled back to 16 bits, but a 24 bit D/A converter would be used instead. In older times (Gravis Ultra Sound and I think older SB AWE) this could easily be spotted by the difference in supported "hardware" channels and "software" channels. A card with a 32 bit sum register and 24 bit DA could support (as above) 16 hardware channels and for example 64 software channels (mixed together in quadrouplets to the 16 hw).
Quoted 6 lines
In our case, such "solution" would have to affect the whole buffer, meaning we would need 3 (or better yet 4) bytes per sample, which would eventually get reduced back to 2 bytes on the way out to the sound card. This seems neither elegant nor memory efficient but would work, and also solves the "a)" problem because we don't need to saturate so an atomic add can be performed on each sample.
Quoted 3 lines
Yes, this solution is good. I've though about it, too. Unfortunately, it adds additional transfers including saturation from the "sum" ring buffer to the DMA buffer of hardware.
I remember you that the main point of dmix existence is the "direct" part.
If we'd need to use an intermediate buffer and a mixing thread, the dmix approach lose our interest.
A solution might be to have a shared parallel sw ring buffer where to store the exact value:
xadd(sw, *src); do { v = *sw; if (v > 0x7fff) s = 0x7fff; else if (v < -0x8000) s = -0x8000; else s = v; *hw = v; } while (unlikely(v != *sw));
Quoted 3 lines
This should solve also the atomicity update.
Comments?
We probably talk about same thing, but in different words. I also don't think that atomicity is an problem when xadd() is atomic (and it is atomic for i386).
Then you need to do the saturation and store to the hardware ring buffer, but if this operation is after xadd() then we don't care about atomicity, because we are 100% sure that we have a valid result.
Algorithm:
while (count) {
atomic_xadd(sum_ring_buffer[idx], local_buffer[idx]);
hw_ring_buffer[idx] = saturate(sum_ring_buffer[idx]);
}
Jaroslav-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Quoted 78 lines · Jaroslav Kysela
Quoted 63 lines · Abramo Bagnara
Quoted 37 lines · Jaroslav Kysela
Quoted 27 lines · Jaroslaw Sobierski
Quoted 8 lines
Quoted 2 lines
b) sum overflow: we can lower volume of samples before sum; I think that hardware works in this way, too
Here I don't understand you. Suppose we have 3 samples to mix:
a =0x7500
b =0x7400
c =0x8300Quoted 7 lines
If you do a + b + c (in this order) you obtain:
d=0
d += a -> 7500
d += b ->0xe900->0x7fff
d += c ->0x02ffwhile the correct result is
0x6c00. You see?AFAIK most hardware does not mix by reducing volume before the sum. On the contrary, it is usually summed "as is" to a wider register, and often even so used. For example, a sound card able to mix 16 chanels of 16 bits would have a 16+4 bits or 24 bit register were the channels are added and no saturation can occur. In good hardware this would not even be downscaled back to 16 bits, but a 24 bit D/A converter would be used instead. In older times (Gravis Ultra Sound and I think older SB AWE) this could easily be spotted by the difference in supported "hardware" channels and "software" channels. A card with a 32 bit sum register and 24 bit DA could support (as above) 16 hardware channels and for example 64 software channels (mixed together in quadrouplets to the 16 hw).
Quoted 6 lines
In our case, such "solution" would have to affect the whole buffer, meaning we would need 3 (or better yet 4) bytes per sample, which would eventually get reduced back to 2 bytes on the way out to the sound card. This seems neither elegant nor memory efficient but would work, and also solves the "a)" problem because we don't need to saturate so an atomic add can be performed on each sample.
Quoted 3 lines
Yes, this solution is good. I've though about it, too. Unfortunately, it adds additional transfers including saturation from the "sum" ring buffer to the DMA buffer of hardware.
I remember you that the main point of dmix existence is the "direct" part.
If we'd need to use an intermediate buffer and a mixing thread, the dmix approach lose our interest.
A solution might be to have a shared parallel sw ring buffer where to store the exact value:
xadd(sw, *src); do { v = *sw; if (v > 0x7fff) s = 0x7fff; else if (v < -0x8000) s = -0x8000; else s = v; *hw = v; } while (unlikely(v != *sw));Quoted 3 lines
This should solve also the atomicity update.
Comments?
We probably talk about same thing, but in different words. I also don't think that atomicity is an problem when
xadd()is atomic (and it is atomic for i386).Then you need to do the saturation and store to the hardware ring buffer, but if this operation is after
xadd()then we don't care about atomicity, because we are 100% sure that we have a valid result.
Quoted 6 lines
Algorithm:
while (count) { atomic_xadd(sum_ring_buffer[idx], local_buffer[idx]); hw_ring_buffer[idx] = saturate(sum_ring_buffer[idx]); }
You're wrong: xadd is atomic but xadd/read/saturation/write is not.
Without the loop I've added you risk to write on hw_ring_buffer an *old* value:
A: B:
xadd
read
xadd
read
saturate
write
saturate
write
Signature · 11 lines
Abramo Bagnara mailto:[email protected] Opera Unica Phone: +39.546.656023 Via Emilia Interna, 140 48014 Castel Bolognese (RA) - Italy ------------------------------------------------------- This sf.net email is sponsored by:ThinkGeek Welcome to geek heaven. http://thinkgeek.com/sf
Quoted 5 lines · Abramo Bagnara
You're wrong: xadd is atomic but xadd/read/saturation/write is not.
Without the loop I've added you risk to write on
hw_ring_bufferan *old* value:
Quoted 9 lines
A: B:
xadd
read
xadd
read
saturate
write
saturate
write
I see, the read/saturate/write must be atomic, too. In this case, it would be better to use a global (or a set of) mutex(es) to lock the hardware ring buffer. The futexes are nice.
Jaroslav
-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Quoted 18 lines · Jaroslav Kysela
Quoted 5 lines · Abramo Bagnara
You're wrong: xadd is atomic but xadd/read/saturation/write is not.
Without the loop I've added you risk to write on
hw_ring_bufferan *old* value:Quoted 9 lines
A: B:
xadd
read
xadd
read
saturate
write
saturate
write
Quoted 3 lines
I see, the read/saturate/write must be atomic, too. In this case, it would be better to use a global (or a set of) mutex(es) to lock the hardware ring buffer. The futexes are nice.
They are nice indeed, but definitely not the right solution here.
Although I don't know if it's the absolute best solution, the 'retry' approach I've proposed is far better and much more efficient.
Signature · 11 lines
Abramo Bagnara mailto:[email protected] Opera Unica Phone: +39.546.656023 Via Emilia Interna, 140 48014 Castel Bolognese (RA) - Italy ------------------------------------------------------- This sf.net email is sponsored by:ThinkGeek Welcome to geek heaven. http://thinkgeek.com/sf
Quoted 5 lines · Jaroslaw Sobierski
Quoted 5 lines
Here I don't understand you. Suppose we have 3 samples to mix:
a =0x7500
b =0x7400
c =0x8300
Quoted 7 lines
Quoted 7 lines
If you do a + b + c (in this order) you obtain:
d=0
d += a -> 7500
d += b ->0xe900->0x7fff
d += c ->0x02ffwhile the correct result is
0x6c00. You see?
Well, but when adding a+b we have no idea that that overlow will be compensated by next very big negative sample. Also mixing signals which already fill 90% of dynamic range is not a good idea. My "fix" is heuristic - it works for occasional _small_ overflows like 0x4100+0x4000 -> 0x7fff is much better than 0x8100.
The idea of dmix as I understand it is that buffer is already in the native format for the sound card. So if sound card supports 24 bit, OK. But then people will start mixing 24 bit samples :-)
Quoted 2 lines
AFAIK most hardware does not mix by reducing volume before the sum. On the contrary, it is usually summed "as is" to a wider register, and often even so
And here our "wider register" is 16bit. That means end users should not expect too much if thay mix full power signals on it.
BTW. If you have uncorrelated signals, then to mix 4 signals it may be good enough to reduce the amplitude of them just factor 2, because power will drop factor 4. Ocassionally there will be overrruns, but 0x7fff limit will make it almost not hearable. Not a correct fix, but I can assure you that it works in standard cases :-)
Best regards,
--
Tomasz Motylewski
BFAD GmbH & Co. KG
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf