Beispiel #1
0
        private static void LoadAssetData()
        {
            for (int x = 0; x < patches.Count; x++)
            {
                for (int y = 0; y < patches[x].Description.GenDescriptions.Length; y++)
                {
                    GeneratorDescriptor genDesc = patches[x].Description.GenDescriptions[y];
                    string assetName            = genDesc.AssetName;
                    string extension            = Path.GetExtension(assetName).ToLower();
                    if (genDesc.SamplerType == WaveformEnum.SampleData && !assetName.Equals("null") && ContainsAsset(assetName, assets) == false)
                    {
                        switch (extension)
                        {
                        case ".wav":
                            using (WaveFileReader wr = new WaveFileReader(File.Open(assetPath + assetName, FileMode.Open, FileAccess.Read)))
                            {
                                assets.Add(new SampleAsset(assetName, wr.ReadWaveFile()));
                            }
                            break;

                        default:
                            throw new Exception(string.Format("Unknown format ({0}), AssetName: {1}, PatchName: {2}", extension, assetName, patches[x].Name));
                        }
                    }
                }
            }
        }
 private void LoadAssetData()
 {
     for (int x = 0; x < patches.Count; x++)
     {
         for (int y = 0; y < patches[x].Description.GenDescriptions.Length; y++)
         {
             GeneratorDescriptor genDesc = patches[x].Description.GenDescriptions[y];
             string assetName            = genDesc.AssetName;
             if (genDesc.SamplerType == WaveformEnum.SampleData && !assetName.Equals("null") && ContainsAsset(assetName, assets) == false)
             {
                 switch (Path.GetExtension(assetName).ToLower())
                 {
                 case ".wav":
                     using (WaveFileReader wr = new WaveFileReader(assetPath + assetName))
                     {
                         assets.Add(new SampleAsset(assetName, wr.ReadWaveFile()));
                     }
                     break;
                 }
             }
         }
     }
 }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="provider"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;

            if (provider != null)
            {
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (editorService == null)
            {
                return(value);
            }

            PcmFileLoaderEditorAttribute att = (PcmFileLoaderEditorAttribute)context.PropertyDescriptor.Attributes[typeof(PcmFileLoaderEditorAttribute)];

            // CurrencyValueEditorForm を使用したプロパティエディタの表示
            using (OpenFileDialog frm = new OpenFileDialog())
            {
                if (att != null)
                {
                    frm.Filter = att.Exts;
                }
                else
                {
                    frm.Filter = "All Files(*.*)|*.*";
                }

                while (true)
                {
                    var result = frm.ShowDialog();
                    if (result != DialogResult.OK)
                    {
                        break;
                    }

                    var fn = frm.FileName;
                    try
                    {
                        if (!Path.GetExtension(fn).Equals(".wav", StringComparison.OrdinalIgnoreCase))
                        {
                            List <byte> buf = new List <byte>();
                            foreach (byte data in File.ReadAllBytes(fn))
                            {
                                buf.Add(data);
                                if (att != null && att.MaxSize != 0 && att.MaxSize == buf.Count)
                                {
                                    break;
                                }
                            }
                            object rvalue = convertRawToRetValue(context, buf.ToArray());
                            if (rvalue != null)
                            {
                                return(rvalue);
                            }
                            return(value);
                        }
                        else
                        {
                            var data = WaveFileReader.ReadWaveFile(fn);

                            if (att.Bits != 0 && att.Bits != data.BitPerSample ||
                                att.Rate != 0 && att.Rate != data.SampleRate ||
                                att.Channels != 0 && att.Channels != data.Channel)
                            {
                                throw new FileLoadException(
                                          string.Format($"Incorrect wave format(Expected Ch={att.Channels} Bit={att.Bits}, Rate={att.Rate},{2})"));
                            }

                            if (data.Data != null)
                            {
                                object rvalue = convertToRetValue(context, data.Data);
                                if (rvalue != null)
                                {
                                    return(rvalue);
                                }
                            }
                            return(value);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.GetType() == typeof(Exception))
                        {
                            throw;
                        }
                        else if (ex.GetType() == typeof(SystemException))
                        {
                            throw;
                        }

                        MessageBox.Show(ex.ToString());
                    }
                }
                return(value);
            }
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="provider"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;

            if (provider != null)
            {
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (editorService == null)
            {
                return(value);
            }

            PcmFileLoaderEditorAttribute att = (PcmFileLoaderEditorAttribute)context.PropertyDescriptor.Attributes[typeof(PcmFileLoaderEditorAttribute)];

            // CurrencyValueEditorForm を使用したプロパティエディタの表示
            using (OpenFileDialog frm = new OpenFileDialog())
            {
                if (att != null)
                {
                    frm.Filter = att.Exts;
                }
                else
                {
                    frm.Filter = "All Files(*.*)|*.*";
                }

                while (true)
                {
                    var result = frm.ShowDialog();
                    if (result != DialogResult.OK)
                    {
                        break;
                    }

                    var fn = frm.FileName;
                    try
                    {
                        if (!Path.GetExtension(fn).Equals(".wav", StringComparison.OrdinalIgnoreCase))
                        {
                            List <byte> buf = new List <byte>();
                            foreach (byte data in File.ReadAllBytes(fn))
                            {
                                buf.Add(data);
                                if (att != null && att.MaxSize != 0 && att.MaxSize == buf.Count)
                                {
                                    break;
                                }
                            }
                            object rvalue = convertRawToRetValue(context, buf.ToArray());
                            if (rvalue != null)
                            {
                                return(rvalue);
                            }
                            return(value);
                        }
                        else
                        {
                            var data = WaveFileReader.ReadWaveFile(fn);

                            if (att.Bits != 0 && att.Bits != data.BitPerSample ||
                                att.Rate != 0 && att.Rate != data.SampleRate ||
                                att.Channels != 0 && att.Channels != data.Channel)
                            {
                                throw new FileLoadException(
                                          string.Format($"Incorrect wave format(Expected Ch={att.Channels} Bit={att.Bits}, Rate={att.Rate},{2})"));
                            }

                            if (data.Data != null)
                            {
                                // byte[] -> short[]
                                List <short> wav = new List <short>();
                                for (int i = 0; i < data.Data.Length; i += 2)
                                {
                                    wav.Add((short)((data.Data[i + 1] << 8) | data.Data[i]));
                                }
                                // Encode
                                int max = 0;
                                if (att != null && att.MaxSize != 0)
                                {
                                    max = att.MaxSize;
                                }
                                byte[] adpcmData = encodeAdpcm(wav.ToArray(), max);

                                switch (context.Instance)
                                {
                                case YM2608Timbre tim:
                                    tim.BaseFreqency = 440d * 55000d / (double)data.SampleRate;
                                    tim.TimbreName   = Path.GetFileNameWithoutExtension(fn);
                                    break;

                                case YM2610BTimbre tim:
                                    tim.BaseFreqency = 440d * 55000d / (double)data.SampleRate;
                                    tim.TimbreName   = Path.GetFileNameWithoutExtension(fn);
                                    break;
                                }

                                // byte[] -> byte[]
                                object rvalue = convertToRetValue(context, adpcmData);
                                if (rvalue != null)
                                {
                                    return(rvalue);
                                }
                            }
                            return(value);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.GetType() == typeof(Exception))
                        {
                            throw;
                        }
                        else if (ex.GetType() == typeof(SystemException))
                        {
                            throw;
                        }

                        MessageBox.Show(ex.ToString());
                    }
                }
                return(value);
            }
        }
Beispiel #5
0
        public void LoadSampleAsset(string assetName, string patchName, string directory)
        {
            string assetNameWithoutExtension;
            string extension;

            if (Path.HasExtension(assetName))
            {
                assetNameWithoutExtension = Path.GetFileNameWithoutExtension(assetName);
                extension = Path.GetExtension(assetName).ToLower();
            }
            else
            {
                assetNameWithoutExtension = assetName;
                assetName += ".wav"; //assume .wav
                extension  = ".wav";
            }
            if (FindSample(assetNameWithoutExtension) == null)
            {
                string waveAssetPath;
                if (CrossPlatformHelper.ResourceExists(assetName))
                {
                    waveAssetPath = assetName; //ex. "asset.wav"
                }
                else if (CrossPlatformHelper.ResourceExists(directory + Path.DirectorySeparatorChar + assetName))
                {
                    waveAssetPath = directory + Path.DirectorySeparatorChar + assetName; //ex. "C:\asset.wav"
                }
                else if (CrossPlatformHelper.ResourceExists(directory + "/SAMPLES/" + assetName))
                {
                    waveAssetPath = directory + "/SAMPLES/" + assetName; //ex. "C:\SAMPLES\asset.wav"
                }
                else if (CrossPlatformHelper.ResourceExists(directory + Path.DirectorySeparatorChar + patchName + Path.DirectorySeparatorChar + assetName))
                {
                    waveAssetPath = directory + Path.DirectorySeparatorChar + patchName + Path.DirectorySeparatorChar + assetName; //ex. "C:\Piano\asset.wav"
                }
                else
                {
                    throw new IOException("Could not find sample asset: (" + assetName + ") required for patch: " + patchName);
                }
                using (BinaryReader reader = new BinaryReader(CrossPlatformHelper.OpenResource(waveAssetPath)))
                {
                    switch (extension)
                    {
                    case ".wav":
                        sampleAssets.Add(new SampleDataAsset(assetNameWithoutExtension, WaveFileReader.ReadWaveFile(reader)));
                        break;
                    }
                }
            }
        }