Changeset 222b176 for src


Ignore:
Timestamp:
Feb 23, 2014, 4:56:54 PM (11 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
870ad70
Parents:
af97786
Message:

src/io/sink_apple_audio.h: add do_multi, preset_samplerate, preset_channels, get_samplerate, and get_channels

Location:
src/io
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/io/sink_apple_audio.c

    raf97786 r222b176  
    11/*
    2   Copyright (C) 2012 Paul Brossier <piem@aubio.org>
     2  Copyright (C) 2012-2014 Paul Brossier <piem@aubio.org>
    33
    44  This file is part of aubio.
     
    2525#include "aubio_priv.h"
    2626#include "fvec.h"
     27#include "fmat.h"
    2728#include "io/sink_apple_audio.h"
    2829
     
    3940char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
    4041
     42uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s);
     43
    4144#define MAX_SIZE 4096 // the maximum number of frames that can be written at a time
    4245
     
    5558aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t samplerate) {
    5659  aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t);
    57   s->samplerate = samplerate;
    58   s->channels = 1;
    5960  s->path = uri;
    6061  s->max_frames = MAX_SIZE;
    6162  s->async = true;
     63
     64  s->samplerate = 0;
     65  s->channels = 0;
     66
     67  // negative samplerate given, abort
     68  if ((sint_t)samplerate < 0) goto beach;
     69  // zero samplerate given. do not open yet
     70  if ((sint_t)samplerate == 0) return s;
     71
     72  s->samplerate = samplerate;
     73  s->channels = 1;
     74
     75  if (aubio_sink_apple_audio_open(s) != AUBIO_OK) {
     76    // open failed, abort
     77    goto beach;
     78  }
     79
     80  return s;
     81beach:
     82  AUBIO_FREE(s);
     83  return NULL;
     84}
     85
     86uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate)
     87{
     88  if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
     89  s->samplerate = samplerate;
     90  // automatically open when both samplerate and channels have been set
     91  if (s->samplerate != 0 && s->channels != 0) {
     92    return aubio_sink_apple_audio_open(s);
     93  }
     94  return AUBIO_OK;
     95}
     96
     97uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels)
     98{
     99  if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
     100  s->channels = channels;
     101  // automatically open when both samplerate and channels have been set
     102  if (s->samplerate != 0 && s->channels != 0) {
     103    return aubio_sink_apple_audio_open(s);
     104  }
     105  return AUBIO_OK;
     106}
     107
     108uint_t aubio_sink_apple_audio_get_samplerate(aubio_sink_apple_audio_t *s)
     109{
     110  return s->samplerate;
     111}
     112
     113uint_t aubio_sink_apple_audio_get_channels(aubio_sink_apple_audio_t *s)
     114{
     115  return s->channels;
     116}
     117
     118uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s) {
     119
     120  if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL;
    62121
    63122  AudioStreamBasicDescription clientFormat;
     
    74133
    75134  AudioFileTypeID fileType = kAudioFileWAVEType;
    76   CFURLRef fileURL = getURLFromPath(uri);
     135  CFURLRef fileURL = getURLFromPath(s->path);
    77136  bool overwrite = true;
    78137  OSStatus err = noErr;
     
    91150    goto beach;
    92151  }
    93   return s;
     152  return AUBIO_OK;
    94153
    95154beach:
    96   AUBIO_FREE(s);
    97   return NULL;
     155  return AUBIO_FAIL;
    98156}
    99157
     
    142200}
    143201
     202void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) {
     203  OSStatus err = noErr;
     204  UInt32 c, v;
     205  short *data = (short*)s->bufferList.mBuffers[0].mData;
     206  if (write > s->max_frames) {
     207    AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
     208    write = s->max_frames;
     209  }
     210  smpl_t **buf = write_data->data;
     211
     212  if (buf) {
     213      for (c = 0; c < s->channels; c++) {
     214          for (v = 0; v < write; v++) {
     215              data[v * s->channels + c] =
     216                  FLOAT_TO_SHORT(buf[c][v]);
     217          }
     218      }
     219  }
     220  if (s->async) {
     221    err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
     222
     223    if (err) {
     224      char_t errorstr[20];
     225      AUBIO_ERROR("sink_apple_audio: error while writing %s "
     226          "in ExtAudioFileWriteAsync (%s), switching to sync\n", s->path,
     227          getPrintableOSStatusError(errorstr, err));
     228      s->async = false;
     229    } else {
     230      return;
     231    }
     232
     233  } else {
     234    err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
     235
     236    if (err) {
     237      char_t errorstr[20];
     238      AUBIO_ERROR("sink_apple_audio: error while writing %s "
     239          "in ExtAudioFileWrite (%s)\n", s->path,
     240          getPrintableOSStatusError(errorstr, err));
     241    }
     242  }
     243  return;
     244}
     245
    144246uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) {
    145247  OSStatus err = noErr;
  • src/io/sink_apple_audio.h

    raf97786 r222b176  
    11/*
    2   Copyright (C) 2012-2013 Paul Brossier <piem@aubio.org>
     2  Copyright (C) 2012-2014 Paul Brossier <piem@aubio.org>
    33
    44  This file is part of aubio.
     
    4040#endif
    4141
     42/** sink_apple_audio object */
    4243typedef struct _aubio_sink_apple_audio_t aubio_sink_apple_audio_t;
    4344
     
    5354  Creates a new sink object.
    5455
     56  If samplerate is set to 0, the creation of the file will be delayed until
     57  both ::aubio_sink_preset_samplerate and ::aubio_sink_preset_channels have
     58  been called.
     59
    5560*/
    5661aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t samplerate);
     62
     63/**
     64
     65  preset sink samplerate
     66
     67  \param s sink, created with ::new_aubio_sink_apple_audio
     68  \param samplerate samplerate to preset the sink to, in Hz
     69
     70  \return 0 on success, 1 on error
     71
     72  Preset the samplerate of the sink. The file should have been created using a
     73  samplerate of 0.
     74
     75  The file will be opened only when both samplerate and channels have been set.
     76
     77*/
     78uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate);
     79
     80/**
     81
     82  preset sink channels
     83
     84  \param s sink, created with ::new_aubio_sink_apple_audio
     85  \param channels number of channels to preset the sink to
     86
     87  \return 0 on success, 1 on error
     88
     89  Preset the samplerate of the sink. The file should have been created using a
     90  samplerate of 0.
     91
     92  The file will be opened only when both samplerate and channels have been set.
     93
     94*/
     95uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels);
     96
     97/**
     98
     99  get samplerate of sink object
     100
     101  \param s sink object, created with ::new_aubio_sink_apple_audio
     102  \return samplerate, in Hz
     103
     104*/
     105uint_t aubio_sink_apple_audio_get_samplerate(aubio_sink_apple_audio_t *s);
     106
     107/**
     108
     109  get channels of sink object
     110
     111  \param s sink object, created with ::new_aubio_sink_apple_audio
     112  \return number of channels
     113
     114*/
     115uint_t aubio_sink_apple_audio_get_channels(aubio_sink_apple_audio_t *s);
    57116
    58117/**
     
    66125*/
    67126void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write);
     127
     128/**
     129
     130  write polyphonic vector of length hop_size to sink
     131
     132  \param s sink, created with ::new_aubio_sink_apple_audio
     133  \param write_data ::fmat_t samples to write to sink
     134  \param write number of frames to write
     135
     136*/
     137void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write);
    68138
    69139/**
Note: See TracChangeset for help on using the changeset viewer.