facebook twitter hatena line email

「Unity/Csharp//変換」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==音からバイトへ変換== C# (CSharp) UnityEngine AudioClip.GetData Examples https://csharp.hotexamples.com/examples/UnityEngine/AudioClip/GetData/php-audioclip-ge...」)
 
 
行1: 行1:
 
==音からバイトへ変換==
 
==音からバイトへ変換==
 +
<pre>
 +
using UnityEngine;
 +
using UnityEngine.UI;
 +
 +
public class SampleScene : MonoBehaviour
 +
{
 +
    [SerializeField]
 +
    readonly AudioSource audioSource;
 +
    [SerializeField]
 +
    readonly Button button;
 +
    // Start is called before the first frame update
 +
    void Start()
 +
    {
 +
        button.onClick.AddListener(ConvertAudio);
 +
    }
 +
    void ConvertAudio()
 +
    {
 +
        // audioSource.Play();
 +
        float[] fs = GetFloatByClip(audioSource.clip);
 +
        byte[] bytes = ToByteArray(fs);
 +
        foreach (byte b in bytes) {
 +
            Debug.Log("b=" + b);
 +
        }
 +
        float[] floats = ToFloatArray(bytes);
 +
        AudioClip audioClip = AudioClip.Create("NewSound", floats.Length, 1, 44100, false);
 +
        audioClip.SetData(floats, 0);
 +
        audioSource.clip = audioClip;
 +
        audioSource.Play();
 +
    }
 +
    float[] GetFloatByClip(AudioClip clip)
 +
    {
 +
        float[] floatData = new float[clip.samples * clip.channels];
 +
        clip.GetData(floatData, 0);
 +
        return floatData;
 +
    }
 +
    // floatsからbytesへ
 +
    byte[] ToByteArray(float[] floatArray)
 +
    {
 +
        int len = floatArray.Length * 4;
 +
        byte[] byteArray = new byte[len];
 +
        int pos = 0;
 +
        foreach (float f in floatArray)
 +
        {
 +
            byte[] data = System.BitConverter.GetBytes(f);
 +
            System.Array.Copy(data, 0, byteArray, pos, 4);
 +
            pos += 4;
 +
        }
 +
        return byteArray;
 +
    }
 +
    // bytesからfloatsへ
 +
    float[] ToFloatArray(byte[] byteArray)
 +
    {
 +
        int len = byteArray.Length / 4;
 +
        float[] floatArray = new float[len];
 +
        for (int i = 0; i < byteArray.Length; i += 4)
 +
        {
 +
            floatArray[i / 4] = System.BitConverter.ToSingle(byteArray, i);
 +
        }
 +
        return floatArray;
 +
    }
 +
}
 +
</pre>
 +
 +
==参考==
 +
音からバイト、バイトから音
 +
https://www.coder.work/article/2877137
  
 
C# (CSharp) UnityEngine AudioClip.GetData Examples
 
C# (CSharp) UnityEngine AudioClip.GetData Examples
 
https://csharp.hotexamples.com/examples/UnityEngine/AudioClip/GetData/php-audioclip-getdata-method-examples.html
 
https://csharp.hotexamples.com/examples/UnityEngine/AudioClip/GetData/php-audioclip-getdata-method-examples.html
 +
 +
Byte[] to AudioClip
 +
https://forum.unity.com/threads/byte-to-audioclip.911723/
 +
 +
WAV byte[] to AudioClip?
 +
https://answers.unity.com/questions/737002/wav-byte-to-audioclip.html
 +
 +
How to convert Unity AudioClip into byte array suitable for Firebase Storage upload
 +
https://stackoverflow.com/questions/55940014/how-to-convert-unity-audioclip-into-byte-array-suitable-for-firebase-storage-upl
 +
 +
How do i play mp3 files in Unity Standalone?
 +
https://gamedev.stackexchange.com/questions/114885/how-do-i-play-mp3-files-in-unity-standalone
 +
 +
create AudioClip from byte[]
 +
https://stackoverflow.com/questions/16078254/create-audioclip-from-byte

2021年12月17日 (金) 17:53時点における最新版

音からバイトへ変換

using UnityEngine;
using UnityEngine.UI;

public class SampleScene : MonoBehaviour
{
    [SerializeField]
    readonly AudioSource audioSource;
    [SerializeField]
    readonly Button button;
    // Start is called before the first frame update
    void Start()
    {
        button.onClick.AddListener(ConvertAudio);
    }
    void ConvertAudio()
    {
        // audioSource.Play();
        float[] fs = GetFloatByClip(audioSource.clip);
        byte[] bytes = ToByteArray(fs);
        foreach (byte b in bytes) {
            Debug.Log("b=" + b);
        }
        float[] floats = ToFloatArray(bytes);
        AudioClip audioClip = AudioClip.Create("NewSound", floats.Length, 1, 44100, false);
        audioClip.SetData(floats, 0);
        audioSource.clip = audioClip;
        audioSource.Play();
    }
    float[] GetFloatByClip(AudioClip clip)
    {
        float[] floatData = new float[clip.samples * clip.channels];
        clip.GetData(floatData, 0);
        return floatData;
    }
    // floatsからbytesへ
    byte[] ToByteArray(float[] floatArray)
    {
        int len = floatArray.Length * 4;
        byte[] byteArray = new byte[len];
        int pos = 0;
        foreach (float f in floatArray)
        {
            byte[] data = System.BitConverter.GetBytes(f);
            System.Array.Copy(data, 0, byteArray, pos, 4);
            pos += 4;
        }
        return byteArray;
    }
    // bytesからfloatsへ
    float[] ToFloatArray(byte[] byteArray)
    {
        int len = byteArray.Length / 4;
        float[] floatArray = new float[len];
        for (int i = 0; i < byteArray.Length; i += 4)
        {
            floatArray[i / 4] = System.BitConverter.ToSingle(byteArray, i);
        }
        return floatArray;
    }
}

参考

音からバイト、バイトから音 https://www.coder.work/article/2877137

C# (CSharp) UnityEngine AudioClip.GetData Examples https://csharp.hotexamples.com/examples/UnityEngine/AudioClip/GetData/php-audioclip-getdata-method-examples.html

Byte[] to AudioClip https://forum.unity.com/threads/byte-to-audioclip.911723/

WAV byte[] to AudioClip? https://answers.unity.com/questions/737002/wav-byte-to-audioclip.html

How to convert Unity AudioClip into byte array suitable for Firebase Storage upload https://stackoverflow.com/questions/55940014/how-to-convert-unity-audioclip-into-byte-array-suitable-for-firebase-storage-upl

How do i play mp3 files in Unity Standalone? https://gamedev.stackexchange.com/questions/114885/how-do-i-play-mp3-files-in-unity-standalone

create AudioClip from byte[] https://stackoverflow.com/questions/16078254/create-audioclip-from-byte