2021. 6. 1. 15:27
Unity/C#
public static void WaveToMP3(string waveFileName, string mp3FileName, NAudio.Lame.LAMEPreset bitRate = NAudio.Lame.LAMEPreset.ABR_128)
{
using (var reader = new NAudio.Wave.WZT.WaveFileReader(waveFileName))
using (var writer = new NAudio.Lame.LameMP3FileWriter(mp3FileName, new NAudio.Wave.WZT.WaveFormat(), bitRate))
reader.CopyTo(writer);
}
public void SaveMp3(string filePath,AudioClip clip)
{
if (clip == null)
{
Debug.LogError("클립없음");
return;
}
var tempPath = Application.temporaryCachePath + "/tempAudio";
//Debug.Log(tempPath);
SavWav.Save(tempPath, clip);
//Debug.Log(filePath);
WaveToMP3(tempPath, filePath);
System.IO.File.Delete(tempPath);
}
볼륨 프로토 타입
float GetVolume(float[] audioData, int index, int samplingRate)
{
if ((audioData == null) || (audioData.Length == 0))
{
return 0;
}
float checkTime = 0.05f;
var startIndex = index;
var length = Mathf.Min(audioData.Length - index, (int)(samplingRate * checkTime));
var sliceArray = audioData.Skip(startIndex).Take(length).ToArray();
//return System.Array.ConvertAll(sliceArray, x => Mathf.Abs(x)).Average();
if ((sliceArray == null) || (sliceArray.Length == 0))
{
return 0;
}
return sliceArray.Max();
}
현재 오디오소스의 볼륨을 가져옴
public AudioSource audioSource;
float GetVolume(float[] audioData, int index, int samplingRate)
{
if ((audioData == null) || (audioData.Length == 0))
{
return 0;
}
float checkTime = 0.05f;
var startIndex = index;
var length = Mathf.Min(audioData.Length - index, (int)(samplingRate * checkTime));
var sliceArray = audioData.Skip(startIndex).Take(length).ToArray();
//return System.Array.ConvertAll(sliceArray, x => Mathf.Abs(x)).Average();
if ((sliceArray == null) || (sliceArray.Length == 0))
{
return 0;
}
return sliceArray.Max();
}
// Update is called once per frame
void Update()
{
var clip = audioSource.clip;
var ratio = audioSource.time / clip.length;
var samplingRate = clip.frequency;
int index = (int)(clip.samples * ratio / clip.channels);
float[] audioData = new float[clip.samples * clip.channels];
audioSource.clip.GetData(audioData, 0);
float volume = GetVolume(audioData, index, samplingRate);
//float volume2 = audioData[index] - audioData[Mathf.Max(0, index - 1)];
var localScale = transform.localScale;
localScale.x= volume;
transform.localScale = localScale;
}
wav로 저장
https://gist.github.com/darktable/2317063
var savePath = SFB.StandaloneFileBrowser.SaveFilePanel("Save File",directory:"", "defaultName", ".wav");
if (savePath == null)
{
return;
}
SavWav.Save(savePath, clip);
mp3로 저장 (Unity3D-save-audioClip-to-MP3)
https://github.com/BeatUpir/Unity3D-save-audioClip-to-MP3
var savePath = SFB.StandaloneFileBrowser.SaveFilePanel("Save File",directory:"", "defaultName", ".mp3");
if (savePath == null)
{
return;
}
EncodeMP3.convert(clip, savePath, bitRate: 128);
mp3로 저장 (Lame-For-Unity)
위에거보단 느리고 불안정
조심할점은 위의 Unity3D-save-audioClip-to-MP3와 충돌한다는 점이다
https://github.com/3wz/Lame-For-Unity
public static void WaveToMP3(string waveFileName, string mp3FileName, NAudio.Lame.LAMEPreset bitRate = NAudio.Lame.LAMEPreset.ABR_128)
{
using (var reader = new NAudio.Wave.WZT.WaveFileReader(waveFileName))
using (var writer = new NAudio.Lame.LameMP3FileWriter(mp3FileName, new NAudio.Wave.WZT.WaveFormat(), bitRate))
reader.CopyTo(writer);
}
public void SaveMp3(string filePath,AudioClip clip)
{
if (clip == null)
{
Debug.LogError("클립없음");
return;
}
var tempPath = Application.temporaryCachePath + $"/tempAudio.wav";
//Debug.Log(tempPath);
SavWav.Save(tempPath, clip);
//Debug.Log(filePath);
WaveToMP3(tempPath, filePath);
System.IO.File.Delete(tempPath);
}
윈도우에서 mp3로드
https://assetstore.unity.com/packages/tools/audio/audioimporter-146746
'Unity > C#' 카테고리의 다른 글
유니티 안드로이드 빌드관련 스크립트 (0) | 2021.06.10 |
---|---|
오큘러스 함수들 (0) | 2021.05.16 |
유니티 에디터 윈도우 (0) | 2021.04.12 |