iPhoneアプリ内で音楽や効果音などを使用するときに、好きな曲だからと簡単に既存の音楽を使うことはできない。アプリ内に含めて配布するのだから相応の使用料が発生する。かと言って作曲し演奏する才能はないし、誰かに曲を提供してもらうのも無償というわけにはなかなかいかない。
それが収益目的のアプリであれば、売上予測の中から音源素材などへの支払額を確保できるのだが、無料アプリなどで収入の予定がない場合には無料のロイヤリティ不要の素材を探して使用することになる。だがなかなか目的に合うものがないのも事実。無料なのだから仕方がないといえば仕方がない。
そんなときにGaragebandを使えばお手軽にそれなりの楽曲を作成することができる。
と言ってもそれなり以上の水準にするにはやはり才能なりが必要なのではあるが……今回はテスト用アプリなのでちょっと自作で我慢しよう。
Garagebandにはかなりの量のループメロディが付属している。これを組み合わせればロック風、民族音楽風などある程度他とは違う楽曲を作成できる。メロディをオリジナルにするのは結構大変みたいだが、リズム系楽器を数種類組み合わせればなんとかなりそうだ。
あまり重ね過ぎてレベルが大きくなり過ぎたり他の音色が聞こえなくなるのはマイナスなので同時に鳴らすのは数種類程度に抑えた方がいいようだ。
とりあえず以下のYouTubeを見て分けもわからず作りました。ループ音源は有料で追加も出来るみたいです。
作曲の理論とかはわかりませんのでそれは他の人にお任せするとして、なんとかそれなりにできたとしてそれをどうやってiPhoneに組み込むかの方法を示して行こうと思います。

では作曲が終わったとして、Garagebandのメニューから「共有>曲をディスクに書き出し…」を選びます。

表示されたシートでファイルのフォーマットを指定します。ここではAAC/高音質/圧縮にしています。エンコード方法はAACかMP3かしか選べません。どちらでも構いません。

そしてこのままでもいいのですが、iOSで扱う最適なファイルフォーマットはcafらしいのでそちらに変換して利用する。
iPhoneアプリ開発のためのサウンドフォーマットまとめ
$ afconvert -f caff -d 'aac ' Loop1.m4a Loop1.caf
ファイルフォーマットは caff 、データフォーマットはどらが適当なのかわからないがAACのままのほうが劣化が少ないのではないかという勝手な想像から'aac 'を指定。音楽データについては勉強が足りないのでよくわからないところ。最適なフォーマットがあれば教えてください。
ここでできたファイルを組み込んだあとのXcodeの使用方法は、別途記述します。
なお、afconvertの使い方は、コマンドプロンプトで「afconvert -h」で表示される。また、使えるファイルフォーマットやデータフォーマットは「afconvert -hf」で表示される。
ちなみに上記の.m4aファイルのサイズは405KBだったが.cafにすると268KBに減った。
$ afconvert -h
Usage:
afconvert [option...] input_file [output_file]
Options may appear before or after the direct arguments. If output_file
is not specified, a name is generated programmatically and the file
is written into the same directory as input_file.
afconvert input_file [-o output_file [option...]]...
Output file options apply to the previous output_file. Other options
may appear anywhere.
General options:
{ -d | --data } data_format[@sample_rate][/format_flags][#frames_per_packet]
[-][BE|LE]{F|[U]I}{8|16|24|32|64} (PCM)
e.g. BEI16 F32@44100
or a data format appropriate to file format (see -hf)
format_flags: hex digits, e.g. '80'
Frames per packet can be specified for some encoders, e.g.: samr#12
A format of "0" specifies the same format as the source file,
with packets copied exactly.
A format of "N" specifies the destination format should be the
native format of the lossless encoded source file (alac, FLAC only)
{ -c | --channels } number_of_channels
add/remove channels without regard to order
{ -l | --channellayout } layout_tag
layout_tag: name of a constant from CoreAudioTypes.h
(prefix "kAudioChannelLayoutTag_" may be omitted)
if specified once, applies to output file; if twice, the first
applies to the input file, the second to the output file
{ -b | --bitrate } total_bit_rate_bps
e.g. 256000 will give you roughly:
for stereo source: 128000 bits per channel
for 5.1 source: 51000 bits per channel
(the .1 channel consumes few bits and can be discounted in the
total bit rate calculation)
{ -q | --quality } codec_quality
codec_quality: 0-127
{ -r | --src-quality } src_quality
src_quality (sample rate converter quality): 0-127 (default is 127)
{ --src-complexity } src_complexity
src_complexity (sample rate converter complexity): line, norm, bats
{ -s | --strategy } strategy
bitrate allocation strategy for encoding an audio track
0 for CBR, 1 for ABR, 2 for VBR_constrained, 3 for VBR
--prime-method method
decode priming method (see AudioConverter.h)
--prime-override samples_prime samples_remain
can be used to override the priming information stored in the source
file to the specified values. If -1 is specified for either, the value
in the file is used.
--no-filler
don't page-align audio data in the output file
--soundcheck-generate
analyze audio, add SoundCheck data to the output file
--codec-manuf codec_manuf
specify the codec with the specified 4-character component manufacturer
code
--dither algorithm
algorithm: 1-3
--mix
enable channel downmixing
{ -u | --userproperty } property value
set an arbitrary AudioConverter property to a given value
property is a four-character code; value is signed 32-bit integer.
e.g. '-u vbrq <sound_quality>' sets the sound quality level
(<sound_quality>: 0-127)
May not be used in a transcoding situation.
-ud property value
identical to -u except only applies to a decoder. Fails if there is no
decoder.
-ue property value
identical to -u except only applies to an encoder. Fails if there is no
encoder.
Input file options:
--read-track track_index
For input files containing multiple tracks, the index (0..n-1)
of the track to read and convert.
--offset number_of_frames
the starting offset in the input file in frames. (The first frame is
frame zero.)
--soundcheck-read
read SoundCheck data from source file and set it on any destination
file(s) of appropriate filetype (.m4a, .caf).
--gapless-before filename
file coming before the current input file of a gapless album
--gapless-after filename
file coming after the current input file of a gapless album
Output file options:
-o filename
specify an (additional) output file.
{ -f | --file } file_format
use -hf for a complete list of supported file/data formats
--condensed-framing field_size_in_bits
specify storage size in bits for externally framed packet sizes.
Supported value is 16 for aac in m4a file format.
Other options:
{ -v | --verbose }
print progress verbosely
{ -t | --tag }
If encoding to CAF, store the source file's format and name in a user
chunk. If decoding from CAF, use the destination format and filename
found in a user chunk.
{ --leaks }
run leaks at the end of the conversion
{ --profile }
collect and print performance information
Help options:
{ -hf | --help-formats }
print a list of supported file/data formats
{ -h | --help }
print this help
$ afconvert -hf
Audio file and data formats:
'3gpp' = 3GP Audio (.3gp)
data_formats: 'Qclp' 'aac ' 'aace' 'aacf' 'aach' 'aacl'
'aacp' 'samr'
'3gp2' = 3GPP-2 Audio (.3g2)
data_formats: 'Qclp' 'aac ' 'aace' 'aacf' 'aach' 'aacl'
'aacp' 'samr'
'adts' = AAC ADTS (.aac, .adts)
data_formats: 'aac ' 'aach' 'aacp'
'ac-3' = AC3 (.ac3)
data_formats: 'ac-3'
'AIFC' = AIFC (.aifc, .aiff, .aif)
data_formats: I8 BEI16 BEI24 BEI32 BEF32 BEF64 UI8 'ulaw'
'alaw' 'MAC3' 'MAC6' 'ima4' 'QDMC' 'QDM2'
'Qclp' 'agsm'
'AIFF' = AIFF (.aiff, .aif)
data_formats: I8 BEI16 BEI24 BEI32
'amrf' = AMR (.amr)
data_formats: 'samr'
'm4af' = Apple MPEG-4 Audio (.m4a, .m4r)
data_formats: 'aac ' 'aace' 'aacf' 'aach' 'aacl' 'aacp'
'ac-3' 'alac' 'paac'
'm4bf' = Apple MPEG-4 AudioBooks (.m4b)
data_formats: 'aac ' 'aace' 'aacf' 'aach' 'aacl' 'aacp'
'paac'
'caff' = CAF (.caf)
data_formats: '.mp1' '.mp2' '.mp3' 'QDM2' 'QDMC' 'Qclp'
'Qclq' 'aac ' 'aace' 'aacf' 'aach' 'aacl'
'aacp' 'ac-3' 'alac' 'alaw' 'dvi8' 'ilbc'
'ima4' I8 BEI16 BEI24 BEI32 BEF32 BEF64 LEI16
LEI24 LEI32 LEF32 LEF64 'ms\x00\x02' 'ms\x00\x11'
'ms\x001' 'ms \x00' 'paac' 'samr' 'ulaw'
'MPG1' = MPEG Layer 1 (.mp1, .mpeg, .mpa)
data_formats: '.mp1'
'MPG2' = MPEG Layer 2 (.mp2, .mpeg, .mpa)
data_formats: '.mp2'
'MPG3' = MPEG Layer 3 (.mp3, .mpeg, .mpa)
data_formats: '.mp3'
'mp4f' = MPEG-4 Audio (.mp4)
data_formats: 'aac ' 'aace' 'aacf' 'aach' 'aacl' 'aacp'
'ac-3'
'NeXT' = NeXT/Sun (.snd, .au)
data_formats: I8 BEI16 BEI24 BEI32 BEF32 BEF64 'ulaw'
'Sd2f' = Sound Designer II (.sd2)
data_formats: I8 BEI16 BEI24 BEI32
'WAVE' = WAVE (.wav)
data_formats: UI8 LEI16 LEI24 LEI32 LEF32 LEF64 'ulaw'
'alaw'
$