Midi: Remove some duplicated code
Introduced new private read/write_midi_settings() and used them in MidiSettingsView and SoftSynth.
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2015 Haiku, Inc. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
#ifndef MIDI_SETTINGS_PRIVATE_H_
|
||||
#define MIDI_SETTINGS_PRIVATE_H_
|
||||
|
||||
#include <StorageDefs.h>
|
||||
#include <SupportDefs.h>
|
||||
|
||||
namespace BPrivate {
|
||||
|
||||
struct midi_settings {
|
||||
char soundfont_file[B_FILE_NAME_LENGTH];
|
||||
};
|
||||
|
||||
status_t read_midi_settings(struct midi_settings* settings);
|
||||
status_t write_midi_settings(struct midi_settings settings);
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif /* MIDI_SETTINGS_PRIVATE_H_ */
|
||||
@@ -18,6 +18,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
|
||||
Midi.cpp
|
||||
MidiGlue.cpp
|
||||
MidiPort.cpp
|
||||
MidiSettings.cpp
|
||||
MidiStore.cpp
|
||||
MidiSynth.cpp
|
||||
MidiSynthFile.cpp
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2015 Haiku, Inc. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
#include <MidiSettings.h>
|
||||
|
||||
#include <File.h>
|
||||
#include <FindDirectory.h>
|
||||
#include <Path.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SETTINGS_FILE "midi"
|
||||
|
||||
namespace BPrivate {
|
||||
|
||||
status_t
|
||||
read_midi_settings(struct midi_settings* settings)
|
||||
{
|
||||
if (settings == NULL)
|
||||
return B_ERROR;
|
||||
|
||||
char buffer[B_FILE_NAME_LENGTH + 128];
|
||||
BPath path;
|
||||
status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
|
||||
if (status != B_OK)
|
||||
return status;
|
||||
|
||||
path.Append("midi");
|
||||
BFile file(path.Path(), B_READ_ONLY);
|
||||
if (file.InitCheck() != B_OK
|
||||
|| file.Read(buffer, sizeof(buffer)) <= 0)
|
||||
return B_ERROR;
|
||||
|
||||
sscanf(buffer, "# Midi Settings\n soundfont = %s\n",
|
||||
settings->soundfont_file);
|
||||
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
write_midi_settings(struct midi_settings settings)
|
||||
{
|
||||
char buffer[B_FILE_NAME_LENGTH + 128];
|
||||
snprintf(buffer, sizeof(buffer), "# Midi Settings\n soundfont = %s\n",
|
||||
settings.soundfont_file);
|
||||
|
||||
BPath path;
|
||||
status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
|
||||
if (status != B_OK)
|
||||
return status;
|
||||
|
||||
path.Append(SETTINGS_FILE);
|
||||
BFile file(path.Path(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
|
||||
|
||||
size_t bufferSize = strlen(buffer);
|
||||
if (file.InitCheck() != B_OK
|
||||
|| file.Write(buffer, bufferSize) != (ssize_t)bufferSize)
|
||||
return B_ERROR;
|
||||
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,9 +21,12 @@
|
||||
#include <NodeInfo.h>
|
||||
#include <Path.h>
|
||||
#include <PathFinder.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <MidiSettings.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "MidiGlue.h" // for MAKE_BIGTIME
|
||||
#include "SoftSynth.h"
|
||||
@@ -108,22 +111,15 @@ BSoftSynth::SetDefaultInstrumentsFile()
|
||||
// MidiSettingsView::_RetrieveSoftSynthList()
|
||||
// We first search for a setting file (or symlink to it)
|
||||
// in the user settings directory
|
||||
char buffer[512];
|
||||
BPath path;
|
||||
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
|
||||
path.Append("midi");
|
||||
BFile file(path.Path(), B_READ_ONLY);
|
||||
if (file.InitCheck() == B_OK
|
||||
&& file.Read(buffer, sizeof(buffer)) > 0) {
|
||||
char soundFont[512];
|
||||
sscanf(buffer, "# Midi Settings\n soundfont = %s\n",
|
||||
soundFont);
|
||||
if (SetInstrumentsFile(soundFont) == B_OK)
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
struct BPrivate::midi_settings settings;
|
||||
if (BPrivate::read_midi_settings(&settings) == B_OK) {
|
||||
if (SetInstrumentsFile(settings.soundfont_file) == B_OK)
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
// Try a well-known (and usually present on a default install) soft synth
|
||||
BPath path;
|
||||
if (find_directory(B_SYNTH_DIRECTORY, &path, false, NULL) == B_OK) {
|
||||
path.Append("synth/TimGM6mb.sf2");
|
||||
if (SetInstrumentsFile(path.Path()) == B_OK)
|
||||
|
||||
@@ -6,7 +6,7 @@ if ! $(TARGET_PLATFORM_HAIKU_COMPATIBLE) {
|
||||
SubDirC++Flags -fmultiple-symbol-spaces ;
|
||||
}
|
||||
|
||||
UsePrivateHeaders media shared ;
|
||||
UsePrivateHeaders media midi shared ;
|
||||
|
||||
Preference Media :
|
||||
Media.cpp
|
||||
@@ -15,7 +15,7 @@ Preference Media :
|
||||
MediaViews.cpp
|
||||
MediaWindow.cpp
|
||||
MidiSettingsView.cpp
|
||||
: media be localestub [ TargetLibsupc++ ]
|
||||
: media midi be localestub [ TargetLibsupc++ ]
|
||||
: media.rdef
|
||||
;
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
|
||||
#include "MidiSettingsView.h"
|
||||
|
||||
#include <MidiSettings.h>
|
||||
|
||||
#include <Box.h>
|
||||
#include <Catalog.h>
|
||||
#include <Directory.h>
|
||||
@@ -27,8 +29,6 @@
|
||||
#undef B_TRANSLATION_CONTEXT
|
||||
#define B_TRANSLATION_CONTEXT "Midi View"
|
||||
|
||||
#define SETTINGS_FILE "midi"
|
||||
|
||||
const static uint32 kSelectSoundFont = 'SeSf';
|
||||
|
||||
|
||||
@@ -126,25 +126,13 @@ MidiSettingsView::_RetrieveSoftSynthList()
|
||||
void
|
||||
MidiSettingsView::_LoadSettings()
|
||||
{
|
||||
// TODO: Duplicated code between here
|
||||
// and BSoftSynth::SetDefaultInstrumentsFile
|
||||
char buffer[512];
|
||||
BPath path;
|
||||
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
|
||||
path.Append(SETTINGS_FILE);
|
||||
BFile file(path.Path(), B_READ_ONLY);
|
||||
if (file.InitCheck() == B_OK) {
|
||||
file.Read(buffer, sizeof(buffer));
|
||||
char soundFont[512];
|
||||
sscanf(buffer, "# Midi Settings\n soundfont = %s\n",
|
||||
soundFont);
|
||||
|
||||
for (int32 i = 0; i < fListView->CountItems(); i++) {
|
||||
BStringItem* item = (BStringItem*)fListView->ItemAt(i);
|
||||
if (!strcmp(item->Text(), soundFont)) {
|
||||
fListView->Select(i);
|
||||
break;
|
||||
}
|
||||
struct BPrivate::midi_settings settings;
|
||||
if (BPrivate::read_midi_settings(&settings) == B_OK) {
|
||||
for (int32 i = 0; i < fListView->CountItems(); i++) {
|
||||
BStringItem* item = (BStringItem*)fListView->ItemAt(i);
|
||||
if (!strcmp(item->Text(), settings.soundfont_file)) {
|
||||
fListView->Select(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,16 +150,8 @@ MidiSettingsView::_SaveSettings()
|
||||
if (item == NULL)
|
||||
return;
|
||||
|
||||
char buffer[512];
|
||||
snprintf(buffer, 512, "# Midi Settings\n soundfont = %s\n",
|
||||
item->Text());
|
||||
|
||||
BPath path;
|
||||
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
|
||||
path.Append(SETTINGS_FILE);
|
||||
BFile file(path.Path(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
|
||||
if (file.InitCheck() == B_OK)
|
||||
file.Write(buffer, strlen(buffer));
|
||||
}
|
||||
struct BPrivate::midi_settings settings;
|
||||
strlcpy(settings.soundfont_file, item->Text(), sizeof(settings.soundfont_file));
|
||||
BPrivate::write_midi_settings(settings);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user