0

I have an app that uses .aupreset files along with .wav samples to define instruments that are loaded into an AUSampler audio unit. The .aupreset files reference the .wav files with absolute paths. As described in Technical Note 2283, if the audio files cannot be found at the original path, then the system searches in the Bundle Directory, NSLibraryDirectory, NSDocumentsDirectory, and NSDownloadsDirectory, in that order.

I can get the instruments working in the app bundle, but now I'm trying to support downloaded instruments. If I put the content in <NSLibraryDirectory>/Sounds/, the AUSampler can't find the .wav samples, but if I put it in <NSDocumentsDirectory>/Sounds/, it works. I don't want to clutter the Documents directory with the instruments though, as that folder shows up in iTunes.

This is the error printed by the AUSampler:

09:27:25.251 ERROR:     [0x19989c310] 372: Failed to locate sample 'file:///Sounds/trance/square-lead/Square-12000Hz.wav'

Also, I can't even create a directory at the NSDownloadsDirectory location - I just get an Operation not permitted error.

Has anyone successfully loaded samples from the Library before?

(This is on iOS 8.)

Jayson
  • 1,689
  • 14
  • 26

2 Answers2

1

I received this explanation from an Apple Engineer:

It turns out the documentation for this is incorrect for iOS: AUSampler will only look in NSDocumentDirectory and the NSDownloadsDirectory in addition to the bundle. These are the recommended locations for storing application-specific data, if you aren't going to store it in the bundle, or you wish to enable users to add additional content to the app.

For OSX, the NSLibraryDirectory is also available.

From my own testing, however, you can't use the NSDownloadsDirectory either, as trying to create the directory returns an error ("The operation couldn’t be completed. Operation not permitted"). So it would appear that the only options on iOS are loading from the bundle or from the Documents directory.

This isn't ideal when you also want to enable iTunes File Sharing for other content, so I did think of another workaround: put the content in an arbitrary location, and when loading the .aupreset file, rewrite all the file references to refer to that location before setting it on the Audio Unit.

Jayson
  • 1,689
  • 14
  • 26
0

Yes, you can definitely load samples from the Library directory. I thought you had to create the library directory, but I was mistaken. It is the Application Support directory I was thinking of.

dave234
  • 4,793
  • 1
  • 14
  • 29
  • I do create the Library directory and successfully copy the files into Library/Sounds, but the AUSampler can't find the samples. – Jayson Jun 30 '15 at 13:03
  • That's strange that it can't find the files. How have you verified the directory exists? – dave234 Jun 30 '15 at 13:07
  • Well, the copy operation succeeds, and if I download the app data to my computer the files are there. I'm also able to access the files directly, but it seems that the AUSampler isn't looking in the Library directory. This is the error I get: `09:27:25.251 ERROR: [0x19989c310] 372: Failed to locate sample 'file:///Sounds/trance/square-lead/Square-12000Hz.wav'` – Jayson Jun 30 '15 at 13:28
  • Are you getting your paths using NSSearchPathForDirectoriesInDomains like the edit on my answer? – dave234 Jun 30 '15 at 13:32
  • Well technically I'm using `[[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] firstObject]` but I double checked and they return the same path/URL. – Jayson Jun 30 '15 at 13:34
  • I checked to make sure I could load sounds from the library directory and everything worked as expected. Maybe a file is missing? Must be some little thing like that. – dave234 Jun 30 '15 at 15:00
  • Hm very strange - do you mind commenting with the path of the file and the file reference so I can compare the structure to mine? – Jayson Jun 30 '15 at 15:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81992/discussion-between-dave-and-jayson). – dave234 Jun 30 '15 at 15:14