static int intercom_exec(struct ast_channel *chan, void *data)
{
	int res = 0;
	struct localuser *u;
	struct ast_frame *f;
	int oreadformat;
	LOCAL_USER_ADD(u);
	/* Remember original read format */
	oreadformat = chan->readformat;
	/* Set mode to signed linear */
	res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
	if (res < 0) {
		ast_log(LOG_WARNING, "Unable to set format to signed linear on channel %s\n", chan->name);
		LOCAL_USER_REMOVE(u);
		return -1;
	}
	/* Read packets from the channel */
	while(!res) {
		res = ast_waitfor(chan, -1);
		if (res > 0) {
			res = 0;
			f = ast_read(chan);
			if (f) {
				if (f->frametype == AST_FRAME_DTMF) {
					ast_frfree(f);
					break;
				} else {
					if (f->frametype == AST_FRAME_VOICE) {
						if (f->subclass == AST_FORMAT_SLINEAR) {
							res = write_audio(f->data, f->datalen);
							if (res > 0)
								res = 0;
						} else
							ast_log(LOG_DEBUG, "Unable to handle non-signed linear frame (%d)\n", f->subclass);
					} 
				}
				ast_frfree(f);
			} else
				res = -1;
		}
	}
	
	if (!res)
		ast_set_read_format(chan, oreadformat);

	LOCAL_USER_REMOVE(u);

	return res;
}
Esempio n. 2
0
    bool play(const char *filename, VFSFile &file)
    {
      force_apply = true;

      try
      {
        MPTWrap mpt(file);

        open_audio(FMT_FLOAT, 44100, 2);

        while(!check_stop())
        {
          unsigned char buffer[65536];
          std::int64_t n;
          int seek_value = check_seek();

          if(seek_value >= 0) mpt.seek(seek_value);

          if(force_apply)
          {
            mpt.set_interpolator(aud_get_int(PACKAGE, SETTING_INTERPOLATOR));
            mpt.set_stereo_separation(aud_get_int(PACKAGE, SETTING_STEREO_SEPARATION));
            force_apply = false;
          }

          n = mpt.read(buffer, sizeof buffer);
          if(n == 0) break;

          write_audio(buffer, n);
        }

        return true;
      }
      catch(MPTWrap::InvalidFile)
      {
        return false;
      }
    }
Esempio n. 3
0
int Asset::write(FileXML *file, 
	int include_index, 
	const char *output_path)
{
	char new_path[BCTEXTLEN];
	char asset_directory[BCTEXTLEN];
	char output_directory[BCTEXTLEN];
	FileSystem fs;

// Make path relative
	fs.extract_dir(asset_directory, path);
	if(output_path && output_path[0]) 
		fs.extract_dir(output_directory, output_path);
	else
		output_directory[0] = 0;

// Asset and EDL are in same directory.  Extract just the name.
	if(!strcmp(asset_directory, output_directory))
	{
		fs.extract_name(new_path, path);
	}
	else
	{
		strcpy(new_path, path);
	}

	file->tag.set_title("ASSET");
	file->tag.set_property("SRC", new_path);
	file->append_tag();
	file->append_newline();

	file->tag.set_title("FOLDER");
	file->tag.set_property("NUMBER", awindow_folder);
	file->append_tag();
	file->tag.set_title("/FOLDER");
	file->append_tag();
	file->append_newline();

// Write the format information
	file->tag.set_title("FORMAT");

	file->tag.set_property("TYPE", 
		File::formattostr(format));
	file->tag.set_property("USE_HEADER", use_header);

	file->append_tag();
	file->tag.set_title("/FORMAT");
	file->append_tag();
	file->append_newline();

// Requiring data to exist caused batch render to lose settings.
// But the only way to know if an asset doesn't have audio or video data 
// is to not write the block.
// So change the block name if the asset doesn't have the data.
	/* if(audio_data) */ write_audio(file);
	/* if(video_data) */ write_video(file);
	if(index_status == 0 && include_index) write_index(file);  // index goes after source

	file->tag.set_title("/ASSET");
	file->append_tag();
	file->append_newline();
	return 0;
}
Esempio n. 4
0
static int32_t Compress_vstate_render_voice(
        Voice_state* vstate,
        Proc_state* proc_state,
        const Device_thread_state* proc_ts,
        const Au_state* au_state,
        const Work_buffers* wbs,
        int32_t buf_start,
        int32_t buf_stop,
        double tempo)
{
    rassert(vstate != NULL);
    rassert(proc_state != NULL);
    rassert(proc_ts != NULL);
    rassert(au_state != NULL);
    rassert(wbs != NULL);
    rassert(buf_start >= 0);
    rassert(buf_stop >= 0);
    rassert(isfinite(tempo));
    rassert(tempo > 0);

    const Device_state* dstate = &proc_state->parent;
    const Proc_compress* compress =
        (const Proc_compress*)proc_state->parent.device->dimpl;

    Compress_vstate* cvstate = (Compress_vstate*)vstate;

    // Get audio input buffers
    const Work_buffer* in_wbs[2] =
    {
        Device_thread_state_get_voice_buffer(
                proc_ts, DEVICE_PORT_TYPE_RECV, PORT_IN_AUDIO_L),
        Device_thread_state_get_voice_buffer(
                proc_ts, DEVICE_PORT_TYPE_RECV, PORT_IN_AUDIO_R),
    };

    // Get audio output buffers
    Work_buffer* out_wbs[2] =
    {
        Device_thread_state_get_voice_buffer(
                proc_ts, DEVICE_PORT_TYPE_SEND, PORT_OUT_AUDIO_L),
        Device_thread_state_get_voice_buffer(
                proc_ts, DEVICE_PORT_TYPE_SEND, PORT_OUT_AUDIO_R),
    };

    // Get level buffers
    Work_buffer* level_wbs[2] =
    {
        Work_buffers_get_buffer_mut(wbs, COMPRESS_WB_LEVEL_L),
        Work_buffers_get_buffer_mut(wbs, COMPRESS_WB_LEVEL_R),
    };

    // Get gain buffer
    Work_buffer* gain_wb = Device_thread_state_get_voice_buffer(
            proc_ts, DEVICE_PORT_TYPE_SEND, PORT_OUT_GAIN);
    if (gain_wb == NULL)
        gain_wb = Work_buffers_get_buffer_mut(wbs, COMPRESS_WB_GAIN);

    Compress_states_update(
            cvstate->cstates,
            compress,
            gain_wb,
            level_wbs,
            in_wbs,
            buf_start,
            buf_stop,
            dstate->audio_rate);

    write_audio(out_wbs, gain_wb, in_wbs, buf_start, buf_stop);

    return buf_stop;
}
Esempio n. 5
0
int
main (int argc, char ** argv)
{
  char * device;
  int number_of_birds;
  double noise_level;
  flock_t flock = NULL;
  bird_data_t data_of_birds;
  int c;
  int count;
  int result;
  int flockfd = -1;
  int audiofd = -1;
  int maxfd;
  fd_set input_fd_set;
  fd_set output_fd_set;
  int play_noise;

  device = DEFAULT_FLOCK_DEVICE;
  number_of_birds = atoi (DEFAULT_NUMBER_OF_BIRDS);
  noise_level = atof (DEFAULT_NOISE_LEVEL);

  /* Parsing arguments. */
  opterr = 0;

  while ((c = getopt (argc, argv, "d:b:n:")) != -1)
    switch (c)
      {
      case 'd':
	device = optarg;
	break;
      case 'b':
	number_of_birds = atoi (optarg);
	break;
      case 'n':
	noise_level = atof (optarg);
	break;
      default:
	break;
      }

  if (argc - optind != 0)
    {
      usage (argv[0]);
      exit (EXIT_FAILURE);
    }

  flock = NULL;
  result = EXIT_SUCCESS;

  get_more_priority ();
  signal (SIGINT, handle_signal);

  fprintf (stderr, "Opening sound card.\n");

  if ((audiofd = open_audio ()) == 0)
    {
      result = EXIT_FAILURE;
      goto terminate;
    }

  play_noise = 0;

  fprintf (stderr, "Preparing flock device: %s, number of birds: %d.\n",
	   device, number_of_birds);

  if ((flock = flock_hl_open (device, number_of_birds,
			      flock_bird_record_mode_position_angles,
			      1, 1)) == NULL)
    {
      result = EXIT_FAILURE;
      goto terminate;
    }

  data_of_birds = (bird_data_t)
    malloc (number_of_birds * sizeof (struct bird_data_s));

  flockfd = flock_get_file_descriptor (flock);
  maxfd = (audiofd < flockfd) ? flockfd : audiofd;
  FD_ZERO (&input_fd_set);
  FD_SET (flockfd, &input_fd_set);
  FD_ZERO (&output_fd_set);
  FD_SET (audiofd, &output_fd_set);

  fprintf (stderr, "Getting data... (Hit Ctrl-C to stop.)\n");

  count = 0;

  /* First values. */
  {
    bird_data_t data;
    int bird;

    if (flock_next_record (flock, 1) == 0)
      {
	fprintf (stderr, "Can't get response from flock.\n");
	result = EXIT_FAILURE;
	goto terminate;
      }

    count++;

    for (bird = 0, data = data_of_birds;
	 bird < number_of_birds;
	 bird++, data++)
      {
	memcpy (&data->rec,
		flock_get_record (flock, bird + 1),
		sizeof (data->rec));
	data->zset = 0;
	data->zcount = 0;
	data->maxdz = 0;
	data->lastbump = 0;
	data->bumpcount = 0;
      }
  }

  while (!terminate)
    {
      fd_set read_fd_set;
      fd_set write_fd_set;

      read_fd_set = input_fd_set;
      write_fd_set = output_fd_set;

      /* Block until new data is available from the flock or we can
         write to the sound card. */
      if (select (maxfd + 1, &read_fd_set, &write_fd_set, NULL, NULL) == -1)
	{
	  perror (__FUNCTION__ ": select");
	  result = EXIT_FAILURE;
	  goto terminate;
	}

      if (FD_ISSET (flockfd, &read_fd_set))
	{
	  bird_data_t data;
	  int bird;

	  if (flock_next_record (flock, 1) == 0)
	    {
	      result = EXIT_FAILURE;
	      goto terminate;
	    }

	  count++;

	  for (bird = 0, data = data_of_birds;
	       bird < number_of_birds;
	       bird++, data++)
	    {
	      double dx, dy, dz;

	      /* Shifting previous record. */
	      memcpy (&data->prev_rec,
		      &data->rec,
		      sizeof (data->rec));

	      /* Copy bird's record. */
	      memcpy (&data->rec,
		      flock_get_record (flock, bird + 1),
		      sizeof (data->rec));

	      dx = data->rec.values.pa.x - data->prev_rec.values.pa.x;
	      dy = data->rec.values.pa.y - data->prev_rec.values.pa.y;
	      dz = data->rec.values.pa.z - data->prev_rec.values.pa.z;

	      if (dx < xthreshold)
		{
		  data->xset = 1;
		  data->xcount = count;
		}

	      if (dz > zthreshold)
		{
		  data->zset = 1;
		  data->zcount = count;
		  if (data->maxdz < dz)
		    data->maxdz = dz;
		}

	      if (!(data->xset && data->zset))
		continue;

	      /* Q: is this really useful? */
	      if (((count - data->xcount) > after_threshold_delay) ||
		  ((count - data->zcount) > after_threshold_delay))
		{
		  data->xset = data->zset = 0;
		  data->maxdz = 0;
		  continue;
		}

	      /* Proposition: delay could depend on maxdz. */
	      if ((dz < 0) && ((count - data->lastbump) > after_bump_delay))
		{
		  fprintf (stderr, "bird %d bumps (%g).\n", bird + 1, data->maxdz);
		  data->xset = data->zset = 0;
		  data->maxdz = 0;
		  data->lastbump = count;
		  data->bumpcount++;

		  play_noise = 1;
		}
	    }
	}

      if (FD_ISSET (audiofd, &write_fd_set))
	{
	  double buffer[AUDIO_BLOCK_SIZE];

	  memset (buffer, 0, sizeof (buffer));

	  if (play_noise)
	    {
	      int i;

	      play_noise = 0;
	      for (i = 0; i < sizeof (buffer) / sizeof (*buffer); i++)
		buffer[i] = ((double) RAND_MAX - 2 * random ()) / 2.0;
	    }

	  write_audio (audiofd, buffer);
	}
    }

 terminate:

  fprintf (stderr, "Exiting.\n");

  if (flock != NULL)
    flock_hl_close (flock);

  if (audiofd != -1)
    close_audio (audiofd);

  return result;
}