Пример #1
0
main() {
    FILE* fgg=NULL;
    long fgg1, fgg2;
    int n;

    if ((fgg=fopen("_finfo_dataset","rt"))==NULL)
    {
      fprintf(stderr,"\nError: Can't find dataset!\n");
      exit(1);
    }

    fscanf(fgg, "%ld", &fgg2);
    fclose(fgg);
  			  
    while(1) {
	n = read(0, sbuf, NSAMPLES*2);
	if ( n < 0 ) {
	    perror("input file");
	    exit(1);
	}
	if ( n == 0 ) break;

        /* FGG */
        for (fgg1=0; fgg1<fgg2; fgg1++)
        {
  	  adpcm_coder(sbuf, abuf, n/2, &state);
	}

	write(1, abuf, n/4);
    }
    fprintf(stderr, "Final valprev=%d, index=%d\n",
	    state.valprev, state.index);
    exit(0);
}
Пример #2
0
/******************************************
* short[] 
*
*/
void audioFSend(void * buffer, int32u_t length, int8u_t types, int32u_t delaytime )
{
	if( WAV16MONE == types)
	{
		int32u_t index=0;
		struct adpcm_state state;

		struct PacketHead myhead;
		myhead.d_port= 1;
		myhead.d_mac = 0x2222;
		myhead.s_port= 0;
		myhead.s_mac = 0x0000;
		myhead.pl_len= 1000;
		myhead.mode  = 0x00;
		//wav audio file head is 44byte
		sendPacket( (void *)buffer, 44, &myhead, delaytime);
		index +=44;

		myhead.mode  = 0x02;
		while( index+ONCELEN*4 < length )
		{
			state.index  =0;
        		state.valprev=0;
			adpcm_coder(&buffer[index], adpcpd, ONCELEN*2, &state);
			index += ONCELEN*4;
			sendPacket( (void *)adpcpd, ONCELEN, &myhead, delaytime );
				
		}
		state.index  =0;
        	state.valprev=0;
		adpcm_coder(&buffer[index], adpcpd, (length-index)/2, &state);
		myhead.mode = 0x0f;
		sendPacket( (void *)adpcpd, (length-index)/4, &myhead, delaytime);
		printf("send audio file finished!\n");
	}
	else
	{
		printf(" data mode not 16mone!\n");
	}

}
Пример #3
0
int
vdvi_encoder(uint16_t idx, u_char *encoder_state, sample *inbuf, coded_unit *c)
{
        int samples, len;

        u_char dvi_buf[80];
        u_char vdvi_buf[160];
        vdvi_state_t *v;

        assert(encoder_state);
        assert(inbuf);
        assert(idx < VDVI_NUM_FORMATS);
        UNUSED(idx);

        v = (vdvi_state_t*)encoder_state;

        /* Transfer state and fix ordering */
        c->state     = (u_char*)block_alloc(sizeof(struct adpcm_state));
        c->state_len = sizeof(struct adpcm_state);
        memcpy(c->state, v->as, sizeof(struct adpcm_state));

        /* Fix coded state for byte ordering */
	((struct adpcm_state*)c->state)->valprev = htons(((struct adpcm_state*)c->state)->valprev);

        samples = cs[idx].format.bytes_per_block * 8 / cs[idx].format.bits_per_sample;

        assert(samples == 160);

        adpcm_coder(inbuf, dvi_buf, samples, v->as);

        bs_attach(v->bs, vdvi_buf, sizeof(vdvi_buf)/sizeof(vdvi_buf[0]));
        memset(vdvi_buf, 0, sizeof(vdvi_buf)/sizeof(vdvi_buf[0]));
        len = vdvi_encode(dvi_buf, 160, v->bs);
        c->data     = (u_char*)block_alloc(len);
        c->data_len = len;
        memcpy(c->data, vdvi_buf, len);

        return len;
}
Пример #4
0
int main(int argc, char *argv[])
{
    int fd,mid;
    int arg;
    int status;
    int i;
    // for encode use
    unsigned char inbuf[FRAME_SIZE*4];  // 2 channels , 16bit data , so *4
    short         inenc[FRAME_SIZE];    // 1 channel, 16bit data, but short type, so *1
    unsigned char encbuf[FRAME_SIZE/2];
    // for decode use
    short         decbuf[FRAME_SIZE];   // decode restore inenc
    unsigned char outbuf[FRAME_SIZE*4]; // restore inbuf 
    // adpcm
    struct adpcm_state enc_state, dec_state;
    //----------------------------------------------------------------------
    fd = open("/dev/dsp", O_RDWR);
    arg = SIZE;
    printf("SIZE:=%d\n",arg);
    if (fd < 0)
    {
        perror("Open /dev/dsp fail");
        exit(1);
    }

    arg = SIZE;
    status = ioctl(fd, SNDCTL_DSP_SETFMT, &arg);
    if (status == -1)
    {
        perror("SNDCTL_DSP_SETFMT ioctl failed");
        exit(1);
    }

    arg    = CHANNELS;
    status = ioctl(fd, SNDCTL_DSP_CHANNELS, &arg);
    if (status == -1)
    {
        perror("SNDCTL_DSP_CHANNELS ioctl failed");
        exit(1);
    }

    ioctl(fd, SOUND_PCM_READ_CHANNELS, &arg);
    if (arg != CHANNELS)
    {
        perror("unable to set channels");
        exit(1);
    }

    arg = RATE;
    status = ioctl(fd, SNDCTL_DSP_SPEED, &arg);
    if (status == -1)
    {
        perror("SNDCTL_DSP_SPEED ioctl failed");
        exit(1);
    }

    if (arg != RATE)
    {
        perror("unable to set rate");
        exit(1);
    }

    mid = open("/dev/mixer",O_RDWR);
    arg = SOUND_MASK_MIC;
    ioctl(mid,SOUND_MIXER_READ_VOLUME,(char *)&arg);
    printf("volume is:%d\n",arg);
    arg = 55000;
    ioctl(mid,SOUND_MIXER_WRITE_VOLUME,(char *)&arg);
    //----------------------------------------------------------------------
    // encode
    enc_state.valprev = 0;
    enc_state.index = 0;
    //----------------------------------------------------------------------
    // decode
    dec_state.valprev = 0;
    dec_state.index = 0;
    //----------------------------------------------------------------------
    while(1)
    {
        // encode
        printf("encode\n");
        read(fd, inbuf, sizeof(inbuf));
        for(i=0;i<FRAME_SIZE*4;i+=4) inenc[i/4] = inbuf[i] + inbuf[i+1]*256;  
        adpcm_coder(inenc, encbuf, FRAME_SIZE, &enc_state);

        // decode
        printf("decode\n");
        adpcm_decoder(encbuf, decbuf, FRAME_SIZE/2, &dec_state); 
        for(i=0;i<FRAME_SIZE;i++)
        {
            outbuf[i*4] = decbuf[i] & 0xff;
            outbuf[i*4+1] = decbuf[i] >> 8;
            outbuf[i*4+2] = decbuf[i] & 0xff;
            outbuf[i*4+3] = decbuf[i] >> 8;
        }
        write(fd, outbuf, sizeof(outbuf)); 
    }
    //----------------------------------------------------------------------

    printf("finished\n");
    return 0;
}  
Пример #5
0
void *mod_cout (void *ptr)
{
  int sockfd;                   /* Socket file descriptor */
  int sent_bytes;               /* Bytes sent */
  struct hostent *hinfo;        /* Host information */
  struct sockaddr_in remote;    /* Remote address information */
  vstr_t vstr;                  /* Data to send */

  int soundfd;                  /* Sound device file descriptor */
  struct adpcm_state state;     /* ADPCM state, see adpcm.c */

  short inbuf[SAMPLES];         /* Input buffer to get sound samples */
  char outbuf[SAMPLES/2];       /* Output buffer to receive ADPCM code */

  rtp_param_t param;            /* RTP parameters */
  vstr_t ctrl;

  char *hostname = (char *) ptr;
  int i;

  DEBUG_MSG("hostname:--%s--\n", hostname);
  fprintf(stderr, "+ Communication output module loaded.\n");
  fprintf(stderr, "+ Sound input module loaded.\n");

  vstr_init (&vstr, RTP_MTU_SIZE);// init vstr size, data part
  vstr_init (&ctrl, RTP_MTU_SIZE);

  /* Initialize ADPCM encoder */
  state.valprev = 0;
  state.index = 0; 

  /* Get target parameters */
  if ((hinfo = gethostbyname(hostname)) == (struct hostent *) NULL) {
    perror("Error resolving host name");
    exit(1);
  }

  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("Error opening socket");
    exit(1);
  }

  remote.sin_family = AF_INET;                  /* Internet protocol */
  remote.sin_addr = *((struct in_addr *) hinfo -> h_addr); /* Address */
  memset(&(remote.sin_zero), '\0', 8);          /* Zero the rest */


  /* Open sound device to read */
  soundfd = open_soundcard (O_RDONLY);

  param.flags = 0;
  param.pt = 3;
  param.len = SAMPLES / 2;
  param.payload = outbuf;

  for (;;) {
    DEBUG_MSG("%s test\n",__FUNCTION__);
    vstr_flush (&ctrl);
    rtcp_send_ctrl (&rtp, &ctrl);
    rtcp_append_sdes (&rtp, &ctrl, SDES_CNAME | SDES_TOOL);

    remote.sin_port = htons(RTCP_PORT);         /* Port */
    sent_bytes = sendto(sockfd, ctrl.head, ctrl.size, 0,
                (struct sockaddr *) &remote, sizeof(struct sockaddr));

    //This will send data to speficy host directly
    for (i = 0; i < 100; i++)
    {
      DEBUG_MSG("%s read data start\n",__FUNCTION__);
      /* Read from sound device */
      read (soundfd, inbuf, sizeof(inbuf));
	DEBUG_MSG("%s read data over\n",__FUNCTION__);
      /* Encodes data */
      adpcm_coder (inbuf, outbuf, SAMPLES, &state);
      /* Create RTP packet */
      vstr_flush (&vstr);
      rtp_send (&rtp, &param, &vstr);

      /* Send to network */
      remote.sin_port = htons(RTP_PORT);        /* Port */
      sent_bytes = sendto(sockfd, vstr.head, vstr.size, 0,
                  (struct sockaddr *) &remote, sizeof(struct sockaddr));
    }
  }
}