Created
December 6, 2013 20:46
-
-
Save tatmos/7831819 to your computer and use it in GitHub Desktop.
PlayStation Mobile Game Jam WInterにて作成した自動生成音楽のソースコード
http://www.youtube.com/watch?v=_obccFFc5bI&feature=share&list=UU12Mw_dKDpVyPy2MLCqYqbg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sce.PlayStation.Core.Audio; | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace SoundManager | |
{ | |
public class SoundManager | |
{ | |
#region soundName | |
public static string[] soundName = { | |
"/Application/sound/ra.wav", | |
"/Application/sound/sq/sq0.wav", | |
"/Application/sound/sq/sq1.wav", | |
"/Application/sound/sq/sq2.wav", | |
"/Application/sound/sq/sq3.wav", | |
"/Application/sound/sq/sq4.wav", | |
"/Application/sound/sq/sq5.wav", | |
"/Application/sound/sq/sq6.wav", | |
"/Application/sound/sq/sq7.wav", | |
"/Application/sound/sq/sq8.wav", | |
"/Application/sound/sq/sq9.wav", | |
"/Application/sound/sq/sq10.wav", | |
"/Application/sound/sq/sq11.wav", | |
"/Application/sound/sq/sq12.wav", | |
"/Application/sound/gb/gb0.wav", | |
"/Application/sound/gb/gb1.wav", | |
"/Application/sound/gb/gb2.wav", | |
"/Application/sound/gb/gb3.wav", | |
"/Application/sound/gb/gb4.wav", | |
"/Application/sound/gb/gb5.wav", | |
"/Application/sound/gb/gb6.wav", | |
"/Application/sound/gb/gb7.wav", | |
"/Application/sound/gb/gb8.wav", | |
"/Application/sound/gb/gb9.wav", | |
"/Application/sound/gb/gb10.wav", | |
"/Application/sound/gb/gb11.wav", | |
"/Application/sound/gb/gb12.wav", | |
"/Application/sound/ts/ts0.wav", | |
"/Application/sound/ts/ts1.wav", | |
"/Application/sound/ts/ts2.wav", | |
"/Application/sound/ts/ts3.wav", | |
"/Application/sound/ts/ts4.wav", | |
"/Application/sound/ts/ts5.wav", | |
"/Application/sound/ts/ts6.wav", | |
"/Application/sound/ts/ts7.wav", | |
"/Application/sound/ts/ts8.wav", | |
"/Application/sound/ts/ts9.wav", | |
"/Application/sound/ts/ts10.wav", | |
"/Application/sound/ts/ts11.wav", | |
"/Application/sound/ts/ts12.wav", | |
}; | |
static int MAX_NOTE_NUM = 13*3; | |
#endregion | |
#region drumName | |
public static string[] drumName = { | |
"/Application/sound/bd.wav", | |
"/Application/sound/sd.wav", | |
}; | |
static int MAX_DRUM_NUM = 2; | |
#endregion | |
#region musicName | |
public static string[] musicName = { | |
"/Application/sound/music/title.wav", | |
"/Application/sound/music/result.wav", | |
"/Application/sound/music/goal.wav", | |
}; | |
static int MAX_MUSIC_NUM = 3; | |
#endregion | |
static int playNoSE = 0; | |
static int playNoDrum = 0; | |
static int playNoMusic = 0; | |
// const,readonly,enum定義 | |
const int MAX_VOICE_NUM = 2; | |
private static Sound[] soundSe = new Sound[MAX_NOTE_NUM]; | |
private static Sound[] soundDrum = new Sound[MAX_DRUM_NUM]; | |
private static Sound[] soundMusic = new Sound[MAX_MUSIC_NUM]; | |
private static SoundPlayer[,] soundPlayerSe = new SoundPlayer[MAX_VOICE_NUM,MAX_NOTE_NUM]; | |
private static SoundPlayer[,] soundPlayerDrum = new SoundPlayer[MAX_VOICE_NUM,MAX_DRUM_NUM]; | |
private static SoundPlayer[,] soundPlayerMusic = new SoundPlayer[MAX_VOICE_NUM,MAX_MUSIC_NUM]; | |
private static SoundPlayer soundBeatTimer; | |
public static string[] musicEventName = { | |
"StartMusic", | |
"StopMusic", | |
"StartTitle", | |
"StartResult" | |
}; | |
public SoundManager () | |
{ | |
} | |
//------------------------------------------------------------ | |
// 初期化 | |
//------------------------------------------------------------ | |
public static void Init() | |
{ | |
rnd = new Random(Environment.TickCount); | |
#region Note | |
for(int j = 0;j<MAX_NOTE_NUM;j++){ | |
soundSe[j] = new Sound(soundName[j]); | |
for(int i =0;i<MAX_VOICE_NUM;i++){ | |
soundPlayerSe[i,j] = soundSe[j].CreatePlayer(); | |
} | |
} | |
#endregion | |
#region Beat | |
soundBeatTimer = new Sound("/Application/sound/beat120_16.wav").CreatePlayer(); | |
#endregion | |
#region Drum | |
for(int j = 0;j<MAX_DRUM_NUM;j++){ | |
soundDrum[j] = new Sound(drumName[j]); | |
for(int i =0;i<MAX_VOICE_NUM;i++){ | |
soundPlayerDrum[i,j] = soundDrum[j].CreatePlayer(); | |
} | |
} | |
#endregion | |
#region Music | |
for(int j = 0;j<MAX_MUSIC_NUM;j++){ | |
soundMusic[j] = new Sound(musicName[j]); | |
for(int i =0;i<MAX_VOICE_NUM;i++){ | |
soundPlayerMusic[i,j] = soundMusic[j].CreatePlayer(); | |
} | |
} | |
#endregion | |
} | |
//------------------------------------------------------------ | |
// Terminate | |
//------------------------------------------------------------ | |
public static void Term() | |
{ | |
for(int j = 0;j<MAX_NOTE_NUM;j++){ | |
for(int i =0;i<soundPlayerSe.Length;i++){ | |
soundPlayerSe[i,j].Stop(); | |
} | |
soundSe[j].Dispose(); | |
} | |
for(int j = 0;j<MAX_DRUM_NUM;j++){ | |
for(int i =0;i<soundPlayerDrum.Length;i++){ | |
soundPlayerDrum[i,j].Stop(); | |
} | |
soundDrum[j].Dispose(); | |
} | |
for(int j = 0;j<MAX_MUSIC_NUM;j++){ | |
for(int i =0;i<soundPlayerMusic.Length;i++){ | |
soundPlayerMusic[i,j].Stop(); | |
} | |
soundMusic[j].Dispose(); | |
} | |
} | |
//------------------------------------------------------------ | |
// SE | |
//------------------------------------------------------------ | |
public static void PlayNote(int soundId,int noteNo) | |
{ | |
//int[] scale_magicstone = {0,4,7}; | |
soundPlayerSe[playNoSE%MAX_VOICE_NUM,soundId].PlaybackRate = (float)Math.Pow(2,(noteNo)/12.0f); | |
soundPlayerSe[playNoSE%MAX_VOICE_NUM,soundId].Play(); | |
playNoSE++; | |
} | |
public static void StopNote(int soundId,int noteNo) | |
{ | |
soundPlayerSe[playNoSE%MAX_VOICE_NUM,soundId].Stop(); | |
} | |
public static void PlayDrum(int soundId,int noteNo) | |
{ | |
//soundPlayerSe[playNo%MAX_SE_NUM,soundId].PlaybackRate = (float)Math.Pow(2,(noteNo)/12.0f); | |
soundPlayerDrum[playNoDrum%MAX_VOICE_NUM,soundId].Play(); | |
playNoDrum++; | |
} | |
static int noteNo = 0; | |
#region Music Property | |
static bool musicUse= false; | |
static float musicSpeed = 0.25f; | |
static int musicNeiroNo = 0; | |
static int muscBaseNote = 0; | |
static int musicMovementNo = 0; | |
#endregion | |
#region StepSequence | |
static int stepNo = 0 ; | |
static int[,] seqNoteList = { | |
{5,99,7,99,3,99,99,-9,}, | |
{5,5,5,5,3,3,3,3,}, | |
{0,2,4,6,8,10,12,14,}, | |
{14,12,10,8,8,10,12,14,} | |
}; | |
static int[,] seqDrumList = | |
{ | |
{0,99,1,0,0,99,1,99,}, | |
{1,1,1,1,0,0,0,0,}, | |
{0,99,1,0,99,1,99,99,}, | |
{0,99,1,1,99,0,0,0,}, | |
}; | |
#endregion | |
#region Solo | |
static int[,,] seqSoloList = | |
{ | |
{ | |
{99,0,0,0, 0,0,0,99, 0,0,0,99, 99,0,0,0,}, | |
{99,0,99,0, 0,99,0,99, 0,99,0,99, 99,99,0,0,}, | |
{99,0,0,99, 0,99,0,99, 99,0,0,99, 99,0,0,99,}, | |
}, | |
{ | |
{99,99,99,99, 0,0,0,0, 99,99,99,99, 99,99,99,99,}, | |
{0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,}, | |
{0,99,0,99, 0,99,0,99, 0,99,0,99, 0,99,0,99,}, | |
}, | |
{ | |
{0,99,99,0, 99,99,0,99, 99,0,99,99, 0,99,99,99,}, | |
{0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,}, | |
{0,99,99,99, 0,99,99,99, 0,99,99,99, 0,99,99,99,} | |
}, | |
{ | |
{0,99,99,99, 99,99,99,99, 99,99,99,99, 99,99,99,99,}, | |
{0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,}, | |
{99,99,99,99, 99,99,0,99, 0,99,99,99, 0,0,0,99,} | |
}, | |
}; | |
static int seqSoloListVariationMax = 3; | |
static int[,] seqSoloScaleList = { | |
{0,2,5,7,9}, | |
{0,1,5,7,9}, | |
{0,1,1,0,1}, | |
{0,3,5,7,9}, | |
{0,0,2,2,0}, | |
}; | |
static int seqSoloScaleListMax = 5; | |
static int seqSoloScaleListVariationMax = 5; | |
#endregion | |
public static bool Update() | |
{ | |
if(musicUse) | |
{ | |
if(stepNo % 8 == 0){ | |
if(rnd.Next(0,32) == 0){ | |
musicSpeed += 0.01f; | |
if(musicSpeed > 2) | |
{ | |
musicSpeed = 0.2f; | |
} | |
if(rnd.Next(0,32) == 0){ | |
musicMovementNo = rnd.Next(0,4); | |
} | |
} | |
if(rnd.Next(0,32) == 0){ | |
musicNeiroNo += 1; | |
if(musicNeiroNo > soundName.Length)musicNeiroNo = 0; | |
} | |
if(rnd.Next(0,32) == 0){ | |
muscBaseNote = rnd.Next(-3,0); | |
} | |
} | |
if(soundBeatTimer.Status == SoundStatus.Stopped){ | |
soundBeatTimer.PlaybackRate = musicSpeed; | |
soundBeatTimer.Play(); | |
#region Note | |
int noteNo = seqNoteList[musicMovementNo,(stepNo++)%8]; | |
if(noteNo != 99){ | |
PlayNote(musicNeiroNo%MAX_NOTE_NUM,noteNo+muscBaseNote); | |
} | |
#endregion | |
#region Drum | |
noteNo = seqDrumList[musicMovementNo,(stepNo)%8]; | |
if(noteNo != 99){ | |
PlayDrum(noteNo,0); | |
} | |
#endregion | |
#region Solo | |
noteNo = seqSoloList[musicMovementNo,(rnd.Next(0,5))%seqSoloListVariationMax,(stepNo)%16]; | |
if(noteNo != 99){ | |
noteNo += seqSoloScaleList[(rnd.Next(0,5))%seqSoloScaleListVariationMax,rnd.Next(0,seqSoloScaleListMax)]; | |
PlayNote((0)%MAX_NOTE_NUM,noteNo+muscBaseNote); | |
} | |
#endregion | |
} | |
} | |
return true; | |
} | |
public static void MusicSpeed(float speed){ | |
musicSpeed = speed; | |
} | |
public static void MusicNeiroNo(int neiroNo) | |
{ | |
musicNeiroNo = neiroNo; | |
} | |
public static void MusicEvent(int eventId) | |
{ | |
switch(eventId) | |
{ | |
case 0:StartMusic();break; | |
case 1:StopMusic();break; | |
case 2:StartMusic_Title();break; | |
case 3:StartMusic_Result();break; | |
case 4:StartMusic_Goal();break; | |
} | |
} | |
static Random rnd; | |
private static void StartMusic() | |
{ | |
rnd = new Random(Environment.TickCount); | |
soundPlayerMusic[0,0].Stop(); // Title Stop | |
soundPlayerMusic[0,1].Stop(); // Title Stop | |
soundPlayerMusic[0,2].Stop(); // Title Stop | |
musicUse = true; | |
} | |
private static void StopMusic() | |
{ | |
soundPlayerMusic[0,0].Stop(); // Title Stop | |
soundPlayerMusic[0,1].Stop(); // Title Stop | |
soundPlayerMusic[0,2].Stop(); // Title Stop | |
musicUse = false; | |
} | |
private static void StartMusic_Title() | |
{ | |
soundPlayerMusic[0,0].Stop(); // Title Stop | |
soundPlayerMusic[0,1].Stop(); // Title Stop | |
soundPlayerMusic[0,2].Stop(); // Title Stop | |
musicUse = false; | |
soundPlayerMusic[0,0].Loop = true; | |
soundPlayerMusic[0,0].Play(); // Title Play | |
} | |
private static void StartMusic_Result() | |
{ | |
soundPlayerMusic[0,0].Stop(); // Title Stop | |
soundPlayerMusic[0,1].Stop(); // Title Stop | |
soundPlayerMusic[0,2].Stop(); // Title Stop | |
musicUse = false; | |
soundPlayerMusic[0,1].Loop = false; | |
soundPlayerMusic[0,1].Play(); // Result Play | |
} | |
private static void StartMusic_Goal() | |
{ | |
soundPlayerMusic[0,0].Stop(); // Title Stop | |
soundPlayerMusic[0,1].Stop(); // Title Stop | |
soundPlayerMusic[0,2].Stop(); // Title Stop | |
musicUse = false; | |
soundPlayerMusic[0,2].Loop = true; | |
soundPlayerMusic[0,2].Play(); // Result Play | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment