title: How to write an array of samples into a 24 bits audio file with AVAudioBuffer?
body: <p>I'm having trouble writing wav files in 24bits with AVAudioEngine in swift.
  For my usage, my input is an array of Float.
  I have the audio format of the input file (retrieved with AVAudioFile).</p>
  
  <p>So, I need to convert my input Float array to a value that will be writable for the buffer. Also, I want to find the right channel to write my data.
  My code is working with 16bit and 32 bit files, but I don't know how to handle 24 bit files...
  Here it is : </p>
  
  <pre class="lang-swift prettyprint-override"><code>  //Static func to write audiofile
    fileprivate func writeAudioFile(to outputURL : URL,
                                    withFormat format : AVAudioFormat,
                                    fromSamples music : [Float] )
    {
      var outputFormatSettings = format.settings
      guard let bufferFormat = AVAudioFormat(settings: outputFormatSettings) else{
        return
      }
  
      var audioFile : AVAudioFile?
      do{
        audioFile = try AVAudioFile(forWriting: outputURL,
                                    settings: outputFormatSettings,
                                    commonFormat: format.commonFormat,
                                    interleaved: true)
      } catch let error as NSError {
        print("error:", error.localizedDescription)
      }
  
      let frameCount = music.count / Int(format.channelCount)
      let outputBuffer = AVAudioPCMBuffer(pcmFormat: bufferFormat,
                                          frameCapacity: AVAudioFrameCount(frameCount))
      //We write the data in the right channel
      guard let bitDepth = (outputFormatSettings["AVLinearPCMBitDepthKey"] as? Int) else {
        return
      }
      switch bitDepth {
      case 16:
        for i in 0..&lt;music.count {
          var floatValue = music[i]
          if(floatValue &gt; 1){
            floatValue = 1
          }
          if(floatValue &lt; -1){
            floatValue = -1
          }
          let value = floatValue * Float(Int16.max)
          outputBuffer?.int16ChannelData!.pointee[i] =  Int16(value)
        }
  case 24:
    //Here I am not sure of what I do ... Could'nt find the right channel !
    for i in 0..&lt;music.count {
      outputBuffer?.floatChannelData!.pointee[i] =  music[i]
    }
      case 32:
        for i in 0..&lt;music.count {
          outputBuffer?.floatChannelData!.pointee[i] = music[i]
        }
      default:
        return
      }
      outputBuffer?.frameLength = AVAudioFrameCount( frameCount )
  
      do{
        try audioFile?.write(from: outputBuffer!)
  
      } catch let error as NSError {
        print("error:", error.localizedDescription)
        return
      }
    }
  </code></pre>
  
  <p>Thanks by advance if someone have an idea of how to handle this !</p>