/*
 * RawOpen
 */
static int RawOpen(void *p_handle,
                   const char **pp_filename_arr,
                   uint64_t filename_arr_len)
{
  t_praw praw=(t_praw)p_handle;
  t_pPiece pPiece;

  praw->Pieces    = filename_arr_len;
  praw->pPieceArr = (t_pPiece) malloc (praw->Pieces * sizeof(t_Piece));
  if (praw->pPieceArr == NULL) return RAW_MEMALLOC_FAILED;
  // Need to set everything to 0 in case an error occurs later and RawClose is
  // called
  memset(praw->pPieceArr,0,praw->Pieces * sizeof(t_Piece));

  praw->TotalSize = 0;
  for (uint64_t i=0; i < praw->Pieces; i++) 
  {
    pPiece = &praw->pPieceArr[i];
    pPiece->pFilename = strdup (pp_filename_arr[i]);
    if (pPiece->pFilename == NULL)
    {
      RawClose(p_handle);
      return RAW_MEMALLOC_FAILED;
    }
    pPiece->pFile = fopen (pPiece->pFilename, "r");
    if (pPiece->pFile == NULL)
    {
      RawClose(p_handle);
      return RAW_FILE_OPEN_FAILED;
    }
    CHK(RawSetCurrentSeekPos(pPiece, 0, SEEK_END))
    pPiece->FileSize = RawGetCurrentSeekPos (pPiece);
    praw->TotalSize  += pPiece->FileSize;
  }

  return RAW_OK;
}
Beispiel #2
0
SexyAL_device *SexyALI_SDL_Open(const char *id, SexyAL_format *format, SexyAL_buffering *buffering)
{
 SexyAL_device *device;
 SDLWrap *sw;
 SDL_AudioSpec desired, obtained;
 const char *env_standalone;
 int iflags;
 int StandAlone = 0;

 env_standalone = getenv("SEXYAL_SDL_STANDALONE");
 if(env_standalone && atoi(env_standalone))
 {
  StandAlone = 1;
  //puts("Standalone");
 }

 iflags = SDL_INIT_AUDIO | SDL_INIT_TIMER;

 #ifdef SDL_INIT_EVENTTHREAD
 iflags |= SDL_INIT_EVENTTHREAD;
 #endif

 if(StandAlone)
 {
  if(SDL_Init(iflags) < 0)
  {
   puts(SDL_GetError());
   return(0);
  }
 }
 else
 {
  //printf("%08x %08x %08x\n", iflags, SDL_WasInit(iflags), SDL_WasInit(iflags) ^ iflags);
  if(SDL_InitSubSystem(SDL_WasInit(iflags) ^ iflags) < 0)
  {
   puts(SDL_GetError());
   return(0);
  }
 }

 sw = (SDLWrap *)calloc(1, sizeof(SDLWrap));

 sw->StandAlone = StandAlone;

 device = (SexyAL_device *)calloc(1, sizeof(SexyAL_device));
 device->private_data = sw;

 memset(&desired, 0, sizeof(SDL_AudioSpec));
 memset(&obtained, 0, sizeof(SDL_AudioSpec));

 int desired_pt = buffering->period_us ? buffering->period_us : 5333;
 int psize = SexyAL_rnearestpow2((int64_t)desired_pt * format->rate / (1000 * 1000), false);

 desired.freq = format->rate;
 desired.format = AUDIO_S16;
 desired.channels = format->channels;
 desired.callback = fillaudio;
 desired.userdata = (void *)device;
 desired.samples = psize;

 if(SDL_OpenAudio(&desired, &obtained) < 0)
 {
  puts(SDL_GetError());
  RawClose(device);
  return(0);
 }

 format->channels = obtained.channels;
 format->rate = obtained.freq;

 if(obtained.format == AUDIO_U8)
  format->sampformat = SEXYAL_FMT_PCMU8;
 else if(obtained.format == AUDIO_S8)
  format->sampformat = SEXYAL_FMT_PCMS8;
 else if(obtained.format == AUDIO_S16LSB || obtained.format == AUDIO_S16MSB)
 {
  format->sampformat = SEXYAL_FMT_PCMS16;

  if(obtained.format != AUDIO_S16SYS)
   format->revbyteorder = 1;
 }
 else if(obtained.format == AUDIO_U16LSB || obtained.format == AUDIO_U16MSB)
 {
  format->sampformat = SEXYAL_FMT_PCMU16;

  if(obtained.format != AUDIO_U16SYS)
   format->revbyteorder = 1;
 }

 if(!buffering->ms) 
  buffering->ms = 100;
 else if(buffering->ms > 1000)
  buffering->ms = 1000;

 sw->EPMaxVal = obtained.samples; //8192;

 sw->BufferSize = (format->rate * buffering->ms / 1000) - obtained.samples * 2;

 if(sw->BufferSize < obtained.samples)
  sw->BufferSize = obtained.samples;

 //printf("%d\n", sw->BufferSize);

 // *2 for safety room, and 30ms extra.
 sw->RealBufferSize = SexyAL_rupow2(sw->BufferSize + sw->EPMaxVal * 2 + ((30 * format->rate + 999) / 1000) );

 sw->BufferIn = sw->BufferRead = sw->BufferWrite = 0;

 buffering->buffer_size = sw->BufferSize;

 buffering->latency = (obtained.samples * 2 + sw->BufferSize);
 buffering->period_size = obtained.samples;
 buffering->bt_gran = 1;

 //printf("%d\n", buffering->latency);

 sw->BufferSize_Raw = sw->BufferSize * format->channels * (format->sampformat >> 4);
 sw->RealBufferSize_Raw = sw->RealBufferSize * format->channels * (format->sampformat >> 4);

 sw->Buffer = malloc(sw->RealBufferSize_Raw);

 memcpy(&device->format, format, sizeof(SexyAL_format));
 memcpy(&device->buffering, buffering, sizeof(SexyAL_buffering));

 device->RawCanWrite = RawCanWrite;
 device->RawWrite = RawWrite;
 device->RawClose = RawClose;
 device->Clear = Clear;
 device->Pause = Pause;

 sw->StartPaused = 1;
 //SDL_PauseAudio(0);
 return(device);
}