|
17 | 17 |
|
18 | 18 | using IBM.Watson.DeveloperCloud.Logging;
|
19 | 19 | using System;
|
20 |
| -using System.Collections; |
21 |
| -using System.Collections.Generic; |
22 | 20 | using System.IO;
|
23 | 21 | using UnityEngine;
|
24 | 22 |
|
25 | 23 | namespace IBM.Watson.DeveloperCloud.Utilities
|
26 | 24 | {
|
| 25 | + /// <summary> |
| 26 | + /// AudioClip helper functions. |
| 27 | + /// </summary> |
| 28 | + public static class AudioClipUtil |
| 29 | + { |
27 | 30 | /// <summary>
|
28 |
| - /// AudioClip helper functions. |
| 31 | + /// This function will combine any number of AudioClips into a single AudioClip. The clips must be the same number of channels |
| 32 | + /// and frequency. |
29 | 33 | /// </summary>
|
30 |
| - public static class AudioClipUtil |
| 34 | + /// <param name="clips">Variable number of AudioClip objects may be provided.</param> |
| 35 | + /// <returns>Returns the resulting AudioClip.</returns> |
| 36 | + public static AudioClip Combine(params AudioClip[] clips) |
31 | 37 | {
|
32 |
| - /// <summary> |
33 |
| - /// This function will combine any number of AudioClips into a single AudioClip. The clips must be the same number of channels |
34 |
| - /// and frequency. |
35 |
| - /// </summary> |
36 |
| - /// <param name="clips">Variable number of AudioClip objects may be provided.</param> |
37 |
| - /// <returns>Returns the resulting AudioClip.</returns> |
38 |
| - public static AudioClip Combine(params AudioClip[] clips) |
| 38 | + if (clips == null || clips.Length == 0) |
| 39 | + return null; |
| 40 | + |
| 41 | + AudioClip firstClip = null; |
| 42 | + |
| 43 | + int length = 0; |
| 44 | + for (int i = 0; i < clips.Length; i++) |
| 45 | + { |
| 46 | + if (clips[i] == null) |
| 47 | + continue; |
| 48 | + |
| 49 | + if (firstClip != null) |
39 | 50 | {
|
40 |
| - if (clips == null || clips.Length == 0) |
41 |
| - return null; |
42 |
| - |
43 |
| - AudioClip firstClip = null; |
44 |
| - |
45 |
| - int length = 0; |
46 |
| - for (int i = 0; i < clips.Length; i++) |
47 |
| - { |
48 |
| - if (clips[i] == null) |
49 |
| - continue; |
50 |
| - |
51 |
| - if (firstClip != null) |
52 |
| - { |
53 |
| - if (firstClip.channels != clips[i].channels |
54 |
| - || firstClip.frequency != clips[i].frequency) |
55 |
| - { |
56 |
| - Log.Error("AudioClipUtil", "Combine() requires clips to have the sample number of channels and same frequency."); |
57 |
| - return null; |
58 |
| - } |
59 |
| - } |
60 |
| - else |
61 |
| - firstClip = clips[i]; |
62 |
| - |
63 |
| - length += clips[i].samples * clips[i].channels; |
64 |
| - } |
65 |
| - |
66 |
| - float[] data = new float[length]; |
67 |
| - length = 0; |
68 |
| - for (int i = 0; i < clips.Length; i++) |
69 |
| - { |
70 |
| - if (clips[i] == null) |
71 |
| - continue; |
72 |
| - |
73 |
| - float[] buffer = new float[clips[i].samples * clips[i].channels]; |
74 |
| - clips[i].GetData(buffer, 0); |
75 |
| - buffer.CopyTo(data, length); |
76 |
| - length += buffer.Length; |
77 |
| - } |
78 |
| - |
79 |
| - if (length == 0) |
80 |
| - return null; |
81 |
| - |
82 |
| - AudioClip result = AudioClip.Create(firstClip.name, length / firstClip.channels, firstClip.channels, firstClip.frequency, false); |
83 |
| - result.SetData(data, 0); |
84 |
| - |
85 |
| - return result; |
| 51 | + if (firstClip.channels != clips[i].channels |
| 52 | + || firstClip.frequency != clips[i].frequency) |
| 53 | + { |
| 54 | + Log.Error("AudioClipUtil", "Combine() requires clips to have the sample number of channels and same frequency."); |
| 55 | + return null; |
| 56 | + } |
86 | 57 | }
|
| 58 | + else |
| 59 | + firstClip = clips[i]; |
87 | 60 |
|
88 |
| - /// <summary> |
89 |
| - /// Returns linear 16-bit audio data for the given AudioClip object. |
90 |
| - /// </summary> |
91 |
| - /// <param name="clip">The AudioClip object.</param> |
92 |
| - /// <returns>A byte array of 16-bit audio data.</returns> |
93 |
| - public static byte[] GetL16(AudioClip clip) |
94 |
| - { |
95 |
| - MemoryStream stream = new MemoryStream(); |
96 |
| - BinaryWriter writer = new BinaryWriter(stream); |
| 61 | + length += clips[i].samples * clips[i].channels; |
| 62 | + } |
97 | 63 |
|
98 |
| - float[] samples = new float[clip.samples * clip.channels]; |
99 |
| - clip.GetData(samples, 0); |
| 64 | + float[] data = new float[length]; |
| 65 | + length = 0; |
| 66 | + for (int i = 0; i < clips.Length; i++) |
| 67 | + { |
| 68 | + if (clips[i] == null) |
| 69 | + continue; |
100 | 70 |
|
101 |
| - float divisor = (1 << 15); |
102 |
| - for (int i = 0; i < samples.Length; ++i) |
103 |
| - writer.Write((short)(samples[i] * divisor)); |
| 71 | + float[] buffer = new float[clips[i].samples * clips[i].channels]; |
| 72 | + clips[i].GetData(buffer, 0); |
| 73 | + buffer.CopyTo(data, length); |
| 74 | + length += buffer.Length; |
| 75 | + } |
104 | 76 |
|
105 |
| - byte[] data = new byte[samples.Length * 2]; |
106 |
| - Array.Copy(stream.GetBuffer(), data, data.Length); |
| 77 | + if (length == 0) |
| 78 | + return null; |
107 | 79 |
|
108 |
| - return data; |
109 |
| - } |
| 80 | + AudioClip result = AudioClip.Create(firstClip.name, length / firstClip.channels, firstClip.channels, firstClip.frequency, false); |
| 81 | + result.SetData(data, 0); |
| 82 | + |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Returns linear 16-bit audio data for the given AudioClip object. |
| 88 | + /// </summary> |
| 89 | + /// <param name="clip">The AudioClip object.</param> |
| 90 | + /// <returns>A byte array of 16-bit audio data.</returns> |
| 91 | + public static byte[] GetL16(AudioClip clip) |
| 92 | + { |
| 93 | + MemoryStream stream = new MemoryStream(); |
| 94 | + BinaryWriter writer = new BinaryWriter(stream); |
| 95 | + |
| 96 | + float[] samples = new float[clip.samples * clip.channels]; |
| 97 | + clip.GetData(samples, 0); |
| 98 | + |
| 99 | + float divisor = (1 << 15); |
| 100 | + for (int i = 0; i < samples.Length; ++i) |
| 101 | + writer.Write((short)(samples[i] * divisor)); |
| 102 | + |
| 103 | + byte[] data = new byte[samples.Length * 2]; |
| 104 | + Array.Copy(stream.GetBuffer(), data, data.Length); |
| 105 | + |
| 106 | + return data; |
110 | 107 | }
|
| 108 | + } |
111 | 109 | }
|
0 commit comments