//*********************************
uint8_t	AUDMEncoder_Lavcodec::getPacket(uint8_t *dest, uint32_t *len, uint32_t *samples)
{
  uint32_t nbout;
  
  *samples = _chunk/_wavheader->channels; //FIXME
  *len = 0;

  if(!refillBuffer(_chunk ))
  {
    return 0; 
  }
        
  if(tmptail-tmphead<_chunk)
  {
    return 0; 
  }

  dither16(&(tmpbuffer[tmphead]),_chunk,_wavheader->channels);

  ADM_assert(tmptail>=tmphead);
  nbout = avcodec_encode_audio(CONTEXT, dest, 5000, (short *) &(tmpbuffer[tmphead]));

  tmphead+=_chunk;
  if (nbout < 0) 
  {
    printf("[Lavcodec] Error !!! : %ld\n", nbout);
    return 0;
  }
  *len=nbout;
  return 1;
}
示例#2
0
	void read(size_type inBytes, void* inData)
		{
		mBytesRead += inBytes;

		while (inBytes > 0)
			{
			int32_t avail = inBytes;
			if (avail + mBufferPos > mBuffer.size())
				avail = mBuffer.size() - mBufferPos;

			if (avail != 0)
				memcpy(inData, &mBuffer[mBufferPos], avail);

			mBufferPos += avail;
			inData = (char*)inData + avail;
			inBytes -= avail;

			if (inBytes == 0)
				return;

			// We still have data left to read. We will either read directly through the protocol
			// or fill up a buffer.
			if (inBytes > mBufferPreferredSize)
				{
				uint32_t bytesRead = mProtocol.read(inBytes, inData, true);
				inBytes -= bytesRead;
				inData = (char*)inData + bytesRead;

				clearBuffer();
				}
			else
				refillBuffer(inBytes);
			}
		}
/**
        \fn getPacket
*/
uint8_t	AUDMEncoder_Aften::getPacket(uint8_t *dest, uint32_t *len, uint32_t *samples)
{
  uint32_t count=0;
  int r;
  void *ptr;
_again:
        *len = 0;
        _chunk=256*6*_wavheader->channels;
        if(!refillBuffer(_chunk ))
        {
          return 0; 
        }
        ptr=(void *)&(tmpbuffer[tmphead]);
        ADM_assert(tmptail>=tmphead);
        reorderChannels(&(tmpbuffer[tmphead]),256*6,_incoming->getChannelMapping(),outputChannelMapping);
        r=aften_encode_frame(_HANDLE, dest,(void *)ptr
#ifdef USE_AFTEN_08_SVN
            ,256*6
#endif
        );
        if(r<0)
        {
          printf("[Aften] Encoding error %d\n",r);
          return 0; 
        }
        
        *samples=256*6;
        *len=r;
        tmphead+=_chunk;
        return 1;
}
/**
    \fn encode
    \brief Get an encoded dca packet
    @param dest [in] Where to write datas
    @param len  [out] Length of encoded datas in bytes
    @param samples [out] Number of samples
    @return true on success, false on error

*/
bool AUDMEncoder_DcaEnc::encode(uint8_t *dest, uint32_t *len, uint32_t *samples)
{

  int32_t nbout;
  int neededSamples=inputSize*wavheader.channels;
  *samples = inputSize;	//FIXME
  *len = 0;
  if(AudioEncoderStopped==_state)
        return false;

    refillBuffer (neededSamples);
    if(AudioEncoderNoInput==_state)
    {
        int left=tmptail-tmphead;
        if (left < neededSamples)
        {
            if(left)
            {
                nbout=send(left,dest);
                tmphead=tmptail;
                ADM_info("[dcaenc]Sending last packet\n");
                goto cont;
            }
              // Flush
              _state=AudioEncoderStopped;
              // flush pad with 0n todo
              if(nbout<0)
              {
                    ADM_warning("Error while flushing dcaenc\n");
                    return false;
              }

              *len=nbout;
              *samples=inputSize;
              ADM_info("[dcaenc] Flushing, last block is %d bytes\n",nbout);
              return true;
    }
  }
  nbout=send(neededSamples,dest);
  tmphead += neededSamples;
cont:
  if (nbout < 0)
    {
      printf ("[dcaenc] Error !!! : %"PRIi32"\n", nbout);
      return false;
    }
  *len = nbout;
  if (!*len)
    *samples =0;
  else
    *samples=inputSize;
  return true;
}
/**
    \fn encode
    \brief Get an encoded mp3 packet
    @param dest [in] Where to write datas
    @param len  [out] Length of encoded datas in bytes
    @param samples [out] Number of samples
    @return true on success, false on error

*/
bool AUDMEncoder_Lame::encode(uint8_t *dest, uint32_t *len, uint32_t *samples)
{

  int32_t nbout;

  *samples = BLOCK_SIZE;	//FIXME
  *len = 0;
  if(AudioEncoderStopped==_state)
        return false;

    refillBuffer (_chunk);
    if(AudioEncoderNoInput==_state)
    {
        int left=tmptail-tmphead;
        if (left < _chunk)
        {
            if(left)
            {
                nbout=send(left,dest);
                tmphead=tmptail;
                ADM_info("[lame]Sending last packet\n");
                goto cont;
            }
              // Flush
              _state=AudioEncoderStopped;
              nbout=lame_encode_flush(MYFLAGS,dest,16*1024);
              if(nbout<0) 
              {
                    ADM_warning("Error while flushing lame\n");
                    return false;   
              }
                    
              *len=nbout;
              *samples=BLOCK_SIZE;  
              ADM_info("[Lame] Flushing, last block is %d bytes\n",nbout);
              return true;
    }
  }
  nbout=send(_chunk,dest);
  tmphead += _chunk;
cont:
  if (nbout < 0)
    {
      printf ("[Lame] Error !!! : %"PRIi32"\n", nbout);
      return false;
    }
  *len = nbout;
  if (!*len)
    *samples = 0;
  else
    *samples=BLOCK_SIZE;
  return true;
}
void DistortionComponent::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
{
    if (&component == curvePoints[0] || &component == curvePoints[1])
    {
        float x1 = (curvePoints[0]->getX() + (0.5f * curvePoints[0]->getWidth())) / (float) getWidth();
        float y1 = ((getHeight() - curvePoints[0]->getY()) - (0.5f * curvePoints[0]->getHeight())) / (float) getHeight();

        float x2 = (curvePoints[1]->getX() + (0.5f * curvePoints[1]->getWidth())) / (float) getWidth();
        float y2 = ((getHeight() - curvePoints[1]->getY()) - (0.5f * curvePoints[1]->getHeight())) / (float) getHeight();
        
        refillBuffer (x1, y1, x2, y2);
    }
}
BitstreamReader::STATUS 
NonSeekFileBitstreamReader::accessBits(u32 *bits, int nbits, NonSeekFileBitstreamReader::ACCESS_MODE acc_mode)
{
	if (nbits < 0 || nbits > 32)
		return STS_ERR_INVALID_ARGUMENTS;

	if (acc_mode != ACC_SKIP)
		*bits = 0;

	State new_state = m_curr_state;

	while (nbits > 0) {
		Buffer *curr_buff = &m_buffs[new_state.buffIdx];

		if (new_state.bytePtr == curr_buff->endPtr) {
			if (curr_buff->eofHit)
				return STS_EOF;

			int new_buff_idx = (new_state.buffIdx + 1) % 2;
			if (m_need_refill) {
				STATUS sts = refillBuffer(new_buff_idx);
				if (sts != STS_OK)
					return sts;

			}

			m_need_refill = (acc_mode != ACC_PEEK);
			new_state.buffIdx = new_buff_idx;
			curr_buff = &m_buffs[new_state.buffIdx];
			new_state.bytePtr = curr_buff->data;
			new_state.bitPtr = 7;
		}

		if (acc_mode != ACC_SKIP) {
			*bits <<= 1;
			*bits |= ((*new_state.bytePtr &  (u8(1) << new_state.bitPtr)) ? 1 : 0);
		}

		if (new_state.bitPtr-- == 0) {
			new_state.bytePtr++;
			new_state.bitPtr = 7;
		}

		nbits--;
	}

	if (acc_mode != ACC_PEEK)
		m_curr_state = new_state;

	return STS_OK;
}
示例#8
0
//==============================================================================
void DRowAudioFilter::parameterChanged (int index, float newValue)
{
	if (index == X1 || index == Y1 || index == X2 || index == Y2)
		refillBuffer();
	else if (index == PREFILTER)
	{
		inFilterL->makeLowPass(currentSampleRate, params[PREFILTER].getValue());
		inFilterR->makeLowPass(currentSampleRate, params[PREFILTER].getValue());
	}
	else if (index == POSTFILTER)
	{
		outFilterL->makeLowPass(currentSampleRate, params[POSTFILTER].getValue());
		outFilterR->makeLowPass(currentSampleRate, params[POSTFILTER].getValue());
	}
}
示例#9
0
//==============================================================================
DRowAudioFilter::DRowAudioFilter()
{
	// set up the parameters with the required limits and units
	
	params[INGAIN].init(parameterNames[INGAIN], UnitDecibels, T("Changes the distortion ammount"),
						0.0, 0.0, 24.0, 0.0);
//	params[INGAIN].setSkewFactorFromMidPoint(0.0);
	
	params[OUTGAIN].init(parameterNames[OUTGAIN], UnitDecibels, T("Changes the output level"),
						 0.0, -6.0, 6.0, 0.0);
	params[OUTGAIN].setSkewFactorFromMidPoint(0.0);
	
	params[PREFILTER].init(parameterNames[PREFILTER], UnitHertz, T("Changes the input filtering"),
					 500.0, 50.0, 5000.0, 500.0);
	params[PREFILTER].setSkewFactor(0.5);
	params[PREFILTER].setStep(1.0);

	params[POSTFILTER].init(parameterNames[POSTFILTER], UnitHertz, T("Changes the output filtering"),
					  500.0, 50.0, 5000.0, 500.0);
	params[POSTFILTER].setSkewFactor(0.5);
	params[POSTFILTER].setStep(1.0);

	
	params[X1].init(parameterNames[X1], UnitGeneric, String::empty,
					0.25, 0.0, 1.0, 0.25);
	
	params[Y1].init(parameterNames[Y1], UnitGeneric, String::empty,
					0.25, 0.0, 1.0, 0.25);
	
	params[X2].init(parameterNames[X2], UnitGeneric, String::empty,
					0.75, 0.0, 1.0, 0.75);
	
	params[Y2].init(parameterNames[Y2], UnitGeneric, String::empty,
					0.75, 0.0, 1.0, 0.75);
	
	// initialiase and fill the buffer
//	distortionBuffer = new float[distortionBufferSize];
	distortionBuffer.calloc(distortionBufferSize);
	refillBuffer();
	
	inFilterL = new OnePoleFilter;
	inFilterR = new OnePoleFilter;
	outFilterL = new OnePoleFilter;
	outFilterR = new OnePoleFilter;
}
uint8_t	AUDMEncoder_PCM::getPacket(uint8_t *dest, uint32_t *len, uint32_t *samples)
{
  uint32_t nbout;
  
  *samples = _chunk; //FIXME
  *len = 0;

  if(!refillBuffer(_chunk ))
  {
    return 0; 
  }
        
  if(tmptail-tmphead<_chunk)
  {
    return 0; 
  }
        // Do in place replace
  dither16(&(tmpbuffer[tmphead]),_chunk,_wavheader->channels);
  if(!revert)
    memcpy(dest,&(tmpbuffer[tmphead]),_chunk*2);
  else
  {
    uint16_t *in,*out,tmp;
    in=(uint16_t*)&(tmpbuffer[tmphead]);
    out=(uint16_t *)dest;
    for(int i=0;i<_chunk;i++)
    {
      tmp=*in++;
      tmp=((tmp&0xff)<<8)+(tmp>>8);
      *out++=tmp;
    }
  }
  tmphead+=_chunk;
  *len=_chunk*2;
  *samples=_chunk/_wavheader->channels;
  return 1;
}
/**
        \fn getPacket
*/
bool 	AUDMEncoder_Twolame::encode(uint8_t *dest, uint32_t *len, uint32_t *samples)
{
  int nbout;
  int channels=wavheader.channels;
  *samples = 1152; //FIXME
  *len = 0;
  ADM_assert(tmptail>=tmphead);
  if(!refillBuffer(_chunk ))
  {
    return false;
  }

  if(tmptail-tmphead<_chunk)
  {
    return false;
  }

  dither16(&(tmpbuffer[tmphead]),_chunk,channels);

  ADM_assert(tmptail>=tmphead);
  if (channels == 1)
  {
    nbout =twolame_encode_buffer(OPTIONS, (int16_t *)&(tmpbuffer[tmphead]),(int16_t *)&(tmpbuffer[tmphead]), _chunk, dest, 16 * 1024);
  }
  else
  {
    nbout = twolame_encode_buffer_interleaved(OPTIONS, (int16_t *)&(tmpbuffer[tmphead]), _chunk/2, dest, 16 * 1024);
  }
  tmphead+=_chunk;
  ADM_assert(tmptail>=tmphead);
  if (nbout < 0) {
    printf("[TwoLame] Error !!! : %d\n", nbout);
    return false;
  }
  *len=nbout;
  return true;
}
uint8_t	AUDMEncoder_Lame::getPacket(uint8_t *dest, uint32_t *len, uint32_t *samples)
{
  int32_t nbout;
  
        *samples = BLOCK_SIZE; //FIXME
        *len = 0;

        if(!refillBuffer(_chunk ))
        {
          return 0; 
        }
        
        if(tmptail-tmphead<_chunk)
        {
          return 0; 
        }
        dither16(&(tmpbuffer[tmphead]),_chunk,_wavheader->channels);
        ADM_assert(tmptail>=tmphead);
        if (_wavheader->channels == 1)
        {
          nbout = lame_encode_buffer(MYFLAGS, (int16_t *)&(tmpbuffer[tmphead]),(int16_t *)&(tmpbuffer[tmphead]), _chunk, dest, 16 * 1024);
          
        }
        else
        {
          nbout = lame_encode_buffer_interleaved(MYFLAGS, (int16_t *)&(tmpbuffer[tmphead]), _chunk/2, dest, 16 * 1024);
        }
        tmphead+=_chunk;
        if (nbout < 0) {
          printf("\n Error !!! : %ld\n", nbout);
          return 0;
        }
        *len=nbout;
        if(!*len) *samples=0;
        //printf("Audio packet : size %u, sample %u\n",*len,*samples);
        return 1;
}
uint8_t	AUDMEncoder_Twolame::getPacket(uint8_t *dest, uint32_t *len, uint32_t *samples)
{
  int nbout;
  
  *samples = 1152; //FIXME
  *len = 0;
  ADM_assert(tmptail>=tmphead);
  if(!refillBuffer(_chunk ))
  {
    return 0; 
  }
        
  if(tmptail-tmphead<_chunk)
  {
    return 0; 
  }

  dither16(&(tmpbuffer[tmphead]),_chunk,_wavheader->channels);

  ADM_assert(tmptail>=tmphead);
  if (_wavheader->channels == 1)
  {
    nbout =twolame_encode_buffer(OPTIONS, (int16_t *)&(tmpbuffer[tmphead]),(int16_t *)&(tmpbuffer[tmphead]), _chunk, dest, 16 * 1024);
  }
  else
  {
    nbout = twolame_encode_buffer_interleaved(OPTIONS, (int16_t *)&(tmpbuffer[tmphead]), _chunk/2, dest, 16 * 1024);
  }
  tmphead+=_chunk;
  ADM_assert(tmptail>=tmphead);
  if (nbout < 0) {
    printf("\n Error !!! : %ld\n", nbout);
    return 0;
  }
  *len=nbout;
  return 1;
}
static pid_t getOneAppProcess(uid_t uid, int appProcessPid, struct ctx *ctx)
{
    if (!ctx->initialized) {
        int ret = initCtx(uid, appProcessPid, ctx);
        if (ret < 0) {
            return ret;
        }
    }

    char *eptr;
    while ((eptr = (char *)memchr(ctx->buf_ptr, '\n', ctx->buf_len)) == NULL) {
        int ret = refillBuffer(ctx);
        if (ret == 0) {
            return -ERANGE;
        }
        if (ret < 0) {
            return ret;
        }
    }

    *eptr = '\0';
    char *pid_eptr = NULL;
    errno = 0;
    long pid = strtol(ctx->buf_ptr, &pid_eptr, 10);
    if (errno != 0) {
        return -errno;
    }
    if (pid_eptr != eptr) {
        return -EINVAL;
    }

    ctx->buf_len -= (eptr - ctx->buf_ptr) + 1;
    ctx->buf_ptr = eptr + 1;

    return (pid_t)pid;
}
//______________________________________________
uint8_t	AUDMEncoder_Faac::getPacket(uint8_t *dest, uint32_t *len, uint32_t *samples)
{
  uint32_t count=0;
_again:
        *samples = _chunk/_wavheader->channels;
        *len = 0;

        if(!refillBuffer(_chunk ))
        {
          return 0; 
        }
        ADM_assert(tmptail>=tmphead);
        reorderChannels(&(tmpbuffer[tmphead]),*samples);
        *len = faacEncEncode(_handle, (int32_t *)&(tmpbuffer[tmphead]), _chunk, dest, FA_BUFFER_SIZE);
        if(!*len) 
        {
          count++;
          if(count<20)
            goto _again;
          *samples=0;
        }
        tmphead+=_chunk;
        return 1;
}
/**
    \fn encode
*/
bool	AUDMEncoder_Lavcodec::encode(uint8_t *dest, uint32_t *len, uint32_t *samples)
{
  uint32_t nbout;
  int retries=16;
again:
  int channels=wavheader.channels;
  *samples = _chunk/channels; //FIXME
  *len = 0;
  if(AudioEncoderStopped==_state)
        return false;

   refillBuffer (_chunk);
   if(AudioEncoderNoInput==_state)
    {
        int left=tmptail-tmphead;
        if (left < _chunk)
        {
            if(left) // Last block
            {
               if(_useFloat==false)
                    dither16(&(tmpbuffer[tmphead]),left,channels);
               ADM_assert(tmptail>=tmphead);
//#warning buffer overread
               nbout = avcodec_encode_audio(CONTEXT, dest, 5000, (short *) &(tmpbuffer[tmphead]));
               tmphead=tmptail;
               *samples = left/channels;
               *len=nbout;
               ADM_info("[Lav] Last audio block\n");
               goto cnt;
            }
              // Flush
               ADM_info("[Lav] Flush\n");
              _state=AudioEncoderStopped;
              if(CONTEXT->codec->capabilities & CODEC_CAP_DELAY)
              {
                  nbout=avcodec_encode_audio(CONTEXT, dest, 5000,NULL);
                  if(nbout<0)
                  {
                        ADM_warning("Error while flushing lame\n");
                        return false;
                  }

                  *len=nbout;
                  *samples=_chunk/channels;
                  ADM_info("[Lav] Flushing, last block is %d bytes\n",nbout);
                  return true;
              }else
              {
              }
              ADM_info("[Lav] No data to flush\n",nbout);
              return true;
        }
    }

  if(_useFloat==false)
    dither16(&(tmpbuffer[tmphead]),_chunk,channels);

  ADM_assert(tmptail>=tmphead);
  nbout = avcodec_encode_audio(CONTEXT, dest, 5000, (short *) &(tmpbuffer[tmphead]));

  tmphead+=_chunk;
cnt:
  if(!nbout && retries)
  {
    retries--;
    ADM_info("Audio encoder (lav): no packet, retrying\n");
    goto again;
  }
  if (nbout < 0)
  {
    ADM_error("[Lavcodec] Error !!! : %"PRIi32"\n", nbout);
    return 0;
  }
  *len=nbout;
  *samples=_chunk/channels;
  return true;
}
/**
    \fn encode
*/
bool	AUDMEncoder_Lavcodec::encode(uint8_t *dest, uint32_t *len, uint32_t *samples)
{
  uint32_t nbout;
  int retries=16;
  bool r;
  int sz;
again:
  int channels=wavheader.channels;
  *samples = _chunk/channels; //FIXME
  *len = 0;
  if(AudioEncoderStopped==_state)
        return false;

   refillBuffer (_chunk);
   
   if(AudioEncoderNoInput==_state)
    {
        int left=tmptail-tmphead;
        if (left < _chunk)
        {
            if(left) // Last block
            {
                
               encodeBlock(left,dest,sz);
               *samples = left/channels;
               *len=sz;
               ADM_info("[Lav] Last audio block\n");
               goto cnt;
            }
              // Flush
               ADM_info("[Lav] Flush\n");
              _state=AudioEncoderStopped;
              if(CONTEXT->codec->capabilities & CODEC_CAP_DELAY)
              {
                  if(false==encodeBlock(0,dest,sz))
                  {
                        ADM_warning("Error while flushing lame\n");
                        return false;                      
                  }
                  *len=sz;
                  *samples=_chunk/channels;
                  ADM_info("[Lav] Flushing, last block is %d bytes\n",nbout);
                  return true;
              }else
              {
              }
              ADM_info("[Lav] No data to flush\n",nbout);
              return true;
        }
    }
   
   r=encodeBlock(_chunk,dest,sz);
   tmphead+=_chunk;
cnt:
  if(!r && retries)
  {
    retries--;
    ADM_info("Audio encoder (lav): no packet, retrying\n");
    goto again;
  }
  *len=sz;
  *samples=_chunk/channels;
  return true;
}
uint8_t	AUDMEncoder_Vorbis::getPacket(uint8_t *dest, uint32_t *len, uint32_t *samples)
{
  uint32_t nbout;
  uint32_t consumed=0;
  float **float_samples;
  ogg_packet op ;

  *len = 0;
  _chunk=1024*_wavheader->channels;
  int count=ROUNDMAX;
// Check that we have packet from previous pass
  while(count--)
  {
    if(!refillBuffer(_chunk ))
    {
      return 0; 
    }
        
    if(tmptail-tmphead<_chunk)
    {
      return 0; 
    }
    
	//printf("Round %d\n",ROUNDMAX-count);
    if(vorbis_analysis_blockout(&VD, &VB) == 1) 
    {
      vorbis_analysis(&VB, NULL);
      vorbis_bitrate_addblock(&VB) ;
	//printf("Blockout\n");
	
      if(vorbis_bitrate_flushpacket(&VD, &op)) 
      {
        memcpy(dest, op.packet,op.bytes);
        *len=op.bytes;
        *samples=op.granulepos-_oldpos;
        _oldpos=op.granulepos;
        //  aprintf("1st packet :sampl:%lu len :%lu sample:%lu abs:%llu\n",*samples,op.bytes,total,op.granulepos);
        return 1;
      }
    }

    
    uint32_t nbSample=(tmptail-tmphead)/_wavheader->channels;
    if(nbSample>1024) nbSample=1024;
    float_samples=vorbis_analysis_buffer(&VD, nbSample) ;
    int index=tmphead;
    // Put our samples in incoming buffer
    reorderChannels(&(tmpbuffer[tmphead]), nbSample,_incoming->getChannelMapping(),outputChannelMapping);
    for (int i = 0; i < nbSample; i++)
      for (int j = 0; j < _wavheader->channels; j++) {
      float_samples[j][i] = tmpbuffer[index++];
      if (float_samples[j][i] > 1) float_samples[j][i] = 1;
      if (float_samples[j][i] < -1) float_samples[j][i] = -1;
      }
      // Buffer full, go go go
      vorbis_analysis_wrote(&VD, nbSample) ;  
      tmphead+=nbSample*_wavheader->channels;	
  }
  return 0;
	
}