fragment_downloaded_cb({"url":"tutorial.html#example-2","fragment":"Example 2\nIn this example, we will expand on the previous example by making one of the input arrays a mono stream, and also scale the mono input stream by a volume. Rather than iterating over each signed 16-bit value, in this example we will iterate over samples, meaning the member size for the stereo arrays is 4, since each array member contains a left and right 16 bit value.\nPiece by piece:\nThis is the same as the previous example, except that the stereo arrays are increased in size to 4. However, we'll use the short type, since Orc does not care what type we use, and short is the type of the array we want to use in the C code.\nThis specifies a parameter, which is an integer that is passed to an Orc function. In the generated C code, parameters are always of type int. There are also float parameters for the floating point equivalent.\nThis specifies a few temporary variables that are used later in the code. These definitions are similar to defining local variables in C code. Note that the size is important: each opcode has specific sizes for source and destination operands, and it is important to match these correctly with temporary variables.\nThis scales the mono input: signed multiply of s2 and volume, giving a 32-bit value, and then a signed right shift by 12. Since the second operand of mulswl is 16-bit, only the lower 16 bits of volume will be used in the multiply. The right shift is effectively the same as dividing by 4096. Thus, a neutral scaling that does not increase or decrease the mono input would correspond to calling the function with a parameter value of 4096.\nThe first instruction is \"convert saturated signed 32-bit to signed 16-bit\", and the second merges the two values of (16 bit) t into the high and low halves of s2_stereo. This duplicates the mono signal into the right and left channels. It is important to use the saturated conversion, since the effective scaling value may have been greater than 1.0, thus the larger values may need to be clipped.\nThe \"x2\" prefix indicates that we want the operation specified to be done twice, first to the upper half of all operands, and again separately to the lower half of all operands. Since addssw is normally a 16-bit operation, the x2 prefix causes it to be a 32-bit operation. And so, it adds the newly created right and left values of the scaled mono signal into the s1 signal.\nThere are several variations of the above program that might be more suitable for a particular application. This function only handles a limited dynamic range of volume scaling factors, however, by changing the shift constant, or turning the shift into a parameter, the dynamic range can be increased significantly.\n"});