/*
 * Purpose: Local output buffer to device export routine for vmix
 */
/*
 * This file is part of Open Sound System
 *
 * Copyright (C) 4Front Technologies 1996-2007.
 *
 * This software is released under CDDL 1.0 source license for operating systems
 * that have their full source code available under the BSD or CDDL licenses.
 *
 * See the COPYING file included in the main directory of this source
 * distribution. Please contact sales@opensound.com for further info.
 */

/*
 * NOTE! This is not the development version of this file. Please download the
 * sources of the latest Open Sound System version from 
 * http://developer.opensound.com/sources if you plan to make any changes
 * to the software.
 */

int i, ch;
float vol;

vol = vmix_db_table[eng->outvol / 5];

for (ch = 0; ch < channels; ch++)
  {
    op = (SAMPLE_TYPE *) outbuf;
    op += ch;

    float vu;
    float *chbuf;

    chbuf = chbufs[ch];

    vu = eng->vu[ch % 2];
    vu = vu / 255.0;

    for (i = 0; i < samples; i++)
      {
	float tmp;

#if 0 && defined(SINE_DEBUG)
	tmp = sine_table[sine_phase[ch]];
	sine_phase[ch] = (sine_phase[ch] + 1 + ch / 2) % SINE_SIZE;
#else
	tmp = VMIX_BYTESWAP (*chbuf++);
#endif

	tmp *= vol;

/*
 * Check for clipping. Decrease volume if necessary.
 */
	if (tmp < -1.0)
	  {
	    vol /= -tmp;
	    eng->outvol--;
	    tmp = -1.0;
	  }
	else if (tmp > 1.0)
	  {
	    vol /= tmp;
	    eng->outvol--;
	    tmp = 1.0;
	  }

	*op = (SAMPLE_TYPE) (tmp * SAMPLE_RANGE);
	op += channels;

	/* VU meter */
	if (tmp < 0.0)
	  tmp = -tmp;
	if (tmp > vu)
	  vu = tmp;
      }

    if (ch < 2)
      {
	vu = vu * 255.0;
	eng->vu[ch] = vu;
      }
  }
