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
|
Midi.cpp
|
||||||
MidiGlue.cpp
|
MidiGlue.cpp
|
||||||
MidiPort.cpp
|
MidiPort.cpp
|
||||||
|
MidiSettings.cpp
|
||||||
MidiStore.cpp
|
MidiStore.cpp
|
||||||
MidiSynth.cpp
|
MidiSynth.cpp
|
||||||
MidiSynthFile.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 <NodeInfo.h>
|
||||||
#include <Path.h>
|
#include <Path.h>
|
||||||
#include <PathFinder.h>
|
#include <PathFinder.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <MidiSettings.h>
|
||||||
|
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "MidiGlue.h" // for MAKE_BIGTIME
|
#include "MidiGlue.h" // for MAKE_BIGTIME
|
||||||
#include "SoftSynth.h"
|
#include "SoftSynth.h"
|
||||||
@@ -108,22 +111,15 @@ BSoftSynth::SetDefaultInstrumentsFile()
|
|||||||
// MidiSettingsView::_RetrieveSoftSynthList()
|
// MidiSettingsView::_RetrieveSoftSynthList()
|
||||||
// We first search for a setting file (or symlink to it)
|
// We first search for a setting file (or symlink to it)
|
||||||
// in the user settings directory
|
// in the user settings directory
|
||||||
char buffer[512];
|
|
||||||
BPath path;
|
struct BPrivate::midi_settings settings;
|
||||||
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
|
if (BPrivate::read_midi_settings(&settings) == B_OK) {
|
||||||
path.Append("midi");
|
if (SetInstrumentsFile(settings.soundfont_file) == B_OK)
|
||||||
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;
|
return B_OK;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Try a well-known (and usually present on a default install) soft synth
|
// 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) {
|
if (find_directory(B_SYNTH_DIRECTORY, &path, false, NULL) == B_OK) {
|
||||||
path.Append("synth/TimGM6mb.sf2");
|
path.Append("synth/TimGM6mb.sf2");
|
||||||
if (SetInstrumentsFile(path.Path()) == B_OK)
|
if (SetInstrumentsFile(path.Path()) == B_OK)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ if ! $(TARGET_PLATFORM_HAIKU_COMPATIBLE) {
|
|||||||
SubDirC++Flags -fmultiple-symbol-spaces ;
|
SubDirC++Flags -fmultiple-symbol-spaces ;
|
||||||
}
|
}
|
||||||
|
|
||||||
UsePrivateHeaders media shared ;
|
UsePrivateHeaders media midi shared ;
|
||||||
|
|
||||||
Preference Media :
|
Preference Media :
|
||||||
Media.cpp
|
Media.cpp
|
||||||
@@ -15,7 +15,7 @@ Preference Media :
|
|||||||
MediaViews.cpp
|
MediaViews.cpp
|
||||||
MediaWindow.cpp
|
MediaWindow.cpp
|
||||||
MidiSettingsView.cpp
|
MidiSettingsView.cpp
|
||||||
: media be localestub [ TargetLibsupc++ ]
|
: media midi be localestub [ TargetLibsupc++ ]
|
||||||
: media.rdef
|
: media.rdef
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
#include "MidiSettingsView.h"
|
#include "MidiSettingsView.h"
|
||||||
|
|
||||||
|
#include <MidiSettings.h>
|
||||||
|
|
||||||
#include <Box.h>
|
#include <Box.h>
|
||||||
#include <Catalog.h>
|
#include <Catalog.h>
|
||||||
#include <Directory.h>
|
#include <Directory.h>
|
||||||
@@ -27,8 +29,6 @@
|
|||||||
#undef B_TRANSLATION_CONTEXT
|
#undef B_TRANSLATION_CONTEXT
|
||||||
#define B_TRANSLATION_CONTEXT "Midi View"
|
#define B_TRANSLATION_CONTEXT "Midi View"
|
||||||
|
|
||||||
#define SETTINGS_FILE "midi"
|
|
||||||
|
|
||||||
const static uint32 kSelectSoundFont = 'SeSf';
|
const static uint32 kSelectSoundFont = 'SeSf';
|
||||||
|
|
||||||
|
|
||||||
@@ -126,28 +126,16 @@ MidiSettingsView::_RetrieveSoftSynthList()
|
|||||||
void
|
void
|
||||||
MidiSettingsView::_LoadSettings()
|
MidiSettingsView::_LoadSettings()
|
||||||
{
|
{
|
||||||
// TODO: Duplicated code between here
|
struct BPrivate::midi_settings settings;
|
||||||
// and BSoftSynth::SetDefaultInstrumentsFile
|
if (BPrivate::read_midi_settings(&settings) == B_OK) {
|
||||||
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++) {
|
for (int32 i = 0; i < fListView->CountItems(); i++) {
|
||||||
BStringItem* item = (BStringItem*)fListView->ItemAt(i);
|
BStringItem* item = (BStringItem*)fListView->ItemAt(i);
|
||||||
if (!strcmp(item->Text(), soundFont)) {
|
if (!strcmp(item->Text(), settings.soundfont_file)) {
|
||||||
fListView->Select(i);
|
fListView->Select(i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -162,16 +150,8 @@ MidiSettingsView::_SaveSettings()
|
|||||||
if (item == NULL)
|
if (item == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
char buffer[512];
|
struct BPrivate::midi_settings settings;
|
||||||
snprintf(buffer, 512, "# Midi Settings\n soundfont = %s\n",
|
strlcpy(settings.soundfont_file, item->Text(), sizeof(settings.soundfont_file));
|
||||||
item->Text());
|
BPrivate::write_midi_settings(settings);
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user