Skip to content

Commit 88d0f36

Browse files
committed
copy sound detector and listener for LGPE
1 parent 9c077d9 commit 88d0f36

File tree

6 files changed

+242
-0
lines changed

6 files changed

+242
-0
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,8 @@ file(GLOB MAIN_SOURCES
13751375
Source/PokemonLA/Resources/PokemonLA_WeatherAndTimeIcons.h
13761376
Source/PokemonLGPE/Commands/PokemonLGPE_DateSpam.cpp
13771377
Source/PokemonLGPE/Commands/PokemonLGPE_DateSpam.h
1378+
Source/PokemonLGPE/Inference/Sounds/PokemonLGPE_ShinySoundDetector.cpp
1379+
Source/PokemonLGPE/Inference/Sounds/PokemonLGPE_ShinySoundDetector.h
13781380
Source/PokemonLGPE/Inference/PokemonLGPE_ShinySymbolDetector.cpp
13791381
Source/PokemonLGPE/Inference/PokemonLGPE_ShinySymbolDetector.h
13801382
Source/PokemonLGPE/Programs/Farming/PokemonLGPE_DailyItemFarmer.cpp
@@ -1385,6 +1387,8 @@ file(GLOB MAIN_SOURCES
13851387
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_FossilRevival.h
13861388
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_GiftReset.cpp
13871389
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_GiftReset.h
1390+
Source/PokemonLGPE/Programs/TestPrograms/PokemonLGPE_SoundListener.cpp
1391+
Source/PokemonLGPE/Programs/TestPrograms/PokemonLGPE_SoundListener.h
13881392
Source/PokemonLGPE/Programs/PokemonLGPE_GameEntry.cpp
13891393
Source/PokemonLGPE/Programs/PokemonLGPE_GameEntry.h
13901394
Source/PokemonLGPE/PokemonLGPE_Panels.cpp

SerialPrograms/SerialPrograms.pro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,11 +674,13 @@ SOURCES += \
674674
Source/PokemonLA/Resources/PokemonLA_PokemonSprites.cpp \
675675
Source/PokemonLA/Resources/PokemonLA_WeatherAndTimeIcons.cpp \
676676
Source/PokemonLGPE/Commands/PokemonLGPE_DateSpam.cpp \
677+
Source/PokemonLGPE/Inference/Sounds/PokemonLGPE_ShinySoundDetector.cpp \
677678
Source/PokemonLGPE/Inference/PokemonLGPE_ShinySymbolDetector.cpp \
678679
Source/PokemonLGPE/Programs/Farming/PokemonLGPE_DailyItemFarmer.cpp \
679680
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_AlolanTrade.cpp \
680681
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_FossilRevival.cpp \
681682
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_GiftReset.cpp \
683+
Source/PokemonLGPE/Programs/TestPrograms/PokemonLGPE_SoundListener.cpp \
682684
Source/PokemonLGPE/Programs/PokemonLGPE_GameEntry.cpp \
683685
Source/PokemonLGPE/PokemonLGPE_Panels.cpp \
684686
Source/PokemonLGPE/PokemonLGPE_Settings.cpp \
@@ -1869,11 +1871,13 @@ HEADERS += \
18691871
Source/PokemonLA/Resources/PokemonLA_PokemonSprites.h \
18701872
Source/PokemonLA/Resources/PokemonLA_WeatherAndTimeIcons.h \
18711873
Source/PokemonLGPE/Commands/PokemonLGPE_DateSpam.h \
1874+
Source/PokemonLGPE/Inference/Sounds/PokemonLGPE_ShinySoundDetector.h \
18721875
Source/PokemonLGPE/Inference/PokemonLGPE_ShinySymbolDetector.h \
18731876
Source/PokemonLGPE/Programs/Farming/PokemonLGPE_DailyItemFarmer.h \
18741877
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_AlolanTrade.h \
18751878
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_FossilRevival.h \
18761879
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_GiftReset.h \
1880+
Source/PokemonLGPE/Programs/TestPrograms/PokemonLGPE_SoundListener.h \
18771881
Source/PokemonLGPE/Programs/PokemonLGPE_GameEntry.h \
18781882
Source/PokemonLGPE/PokemonLGPE_Panels.h \
18791883
Source/PokemonLGPE/PokemonLGPE_Settings.h \
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Shiny Sound Detector
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "CommonTools/Audio/AudioTemplateCache.h"
8+
#include "CommonTools/Audio/SpectrogramMatcher.h"
9+
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"
10+
#include "PokemonLGPE/PokemonLGPE_Settings.h"
11+
#include "PokemonLGPE_ShinySoundDetector.h"
12+
13+
namespace PokemonAutomation{
14+
namespace NintendoSwitch{
15+
namespace PokemonLGPE{
16+
17+
18+
ShinySoundDetector::ShinySoundDetector(Logger& logger, DetectedCallback detected_callback)
19+
// Use a yellow as the detection color because the shiny animation is yellow.
20+
: AudioPerSpectrumDetectorBase(
21+
logger,
22+
"ShinySoundDetector",
23+
"Shiny sound",
24+
COLOR_YELLOW,
25+
detected_callback
26+
)
27+
{}
28+
29+
30+
float ShinySoundDetector::get_score_threshold() const{
31+
return (float)GameSettings::instance().SHINY_SOUND_THRESHOLD;
32+
}
33+
34+
std::unique_ptr<SpectrogramMatcher> ShinySoundDetector::build_spectrogram_matcher(size_t sample_rate){
35+
return std::make_unique<SpectrogramMatcher>(
36+
"Shiny Sound",
37+
AudioTemplateCache::instance().get_throw("PokemonLGPE/ShinySound", sample_rate),
38+
SpectrogramMatcher::Mode::SPIKE_CONV, sample_rate,
39+
GameSettings::instance().SHINY_SOUND_LOW_FREQUENCY
40+
);
41+
}
42+
43+
44+
45+
}
46+
}
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Shiny Sound Detector
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_PokemonLGPE_ShinySoundDetector_H
8+
#define PokemonAutomation_PokemonLGPE_ShinySoundDetector_H
9+
10+
#include "CommonTools/Audio/AudioPerSpectrumDetectorBase.h"
11+
12+
namespace PokemonAutomation{
13+
namespace NintendoSwitch{
14+
namespace PokemonLGPE{
15+
16+
17+
class ShinySoundDetector : public AudioPerSpectrumDetectorBase{
18+
public:
19+
// Warning: The callback will be called from the audio inference thread.
20+
ShinySoundDetector(Logger& logger, DetectedCallback detected_callback);
21+
22+
// Implement AudioPerSpectrumDetectorBase::get_score_threshold()
23+
virtual float get_score_threshold() const override;
24+
25+
protected:
26+
// Implement AudioPerSpectrumDetectorBase::build_spectrogram_matcher()
27+
virtual std::unique_ptr<SpectrogramMatcher> build_spectrogram_matcher(size_t sample_rate) override;
28+
};
29+
30+
31+
32+
33+
}
34+
}
35+
}
36+
#endif
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* Sound Listener
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include <float.h>
8+
#include <chrono>
9+
#include <map>
10+
#include <iostream>
11+
#include <fstream>
12+
#include <sstream>
13+
#include "Common/Cpp/Exceptions.h"
14+
#include "Common/Cpp/Containers/AlignedVector.tpp"
15+
#include "CommonFramework/AudioPipeline/AudioFeed.h"
16+
#include "CommonFramework/AudioPipeline/AudioTemplate.h"
17+
#include "CommonTools/Audio/AudioTemplateCache.h"
18+
#include "CommonTools/Audio/SpectrogramMatcher.h"
19+
#include "CommonTools/Async/InferenceSession.h"
20+
#include "Pokemon/Pokemon_Strings.h"
21+
#include "PokemonLGPE/Inference/Sounds/PokemonLGPE_ShinySoundDetector.h"
22+
#include "PokemonLGPE_SoundListener.h"
23+
24+
25+
namespace PokemonAutomation{
26+
namespace NintendoSwitch{
27+
namespace PokemonLGPE{
28+
using namespace Pokemon;
29+
30+
31+
SoundListener_Descriptor::SoundListener_Descriptor()
32+
: SingleSwitchProgramDescriptor(
33+
"PokemonLGPE:SoundListener",
34+
STRING_POKEMON + " LGPE", "Sound Listener",
35+
"",
36+
"Test sound detectors listening to audio stream.",
37+
FeedbackType::REQUIRED, AllowCommandsWhenRunning::ENABLE_COMMANDS,
38+
{ControllerFeature::NintendoSwitch_ProController}
39+
)
40+
{}
41+
42+
43+
SoundListener::SoundListener()
44+
: SOUND_TYPE("<b>Which Sound to Detect</b>",
45+
{
46+
{SoundType::SHINY, "shiny", "Shiny Sound"},
47+
},
48+
LockMode::LOCK_WHILE_RUNNING,
49+
SoundType::SHINY
50+
)
51+
, STOP_ON_DETECTED_SOUND(
52+
"<b>Stop on the detected sound</b><br>Stop program when the sound is detected.",
53+
LockMode::LOCK_WHILE_RUNNING,
54+
false
55+
)
56+
{
57+
PA_ADD_OPTION(SOUND_TYPE);
58+
PA_ADD_OPTION(STOP_ON_DETECTED_SOUND);
59+
}
60+
61+
void SoundListener::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){
62+
std::cout << "Running audio test program." << std::endl;
63+
64+
std::shared_ptr<AudioInferenceCallback> detector;
65+
auto action = [&](float error_coefficient) -> bool{
66+
// This lambda function will be called when the sound is detected.
67+
// Its return will determine whether to stop the program:
68+
return STOP_ON_DETECTED_SOUND;
69+
};
70+
71+
SoundType type = SOUND_TYPE;
72+
switch (type){
73+
case SoundType::SHINY:
74+
detector = std::make_shared<ShinySoundDetector>(env.console, action);
75+
break;
76+
default:
77+
throw InternalProgramError(
78+
&env.logger(), PA_CURRENT_FUNCTION,
79+
"Not such sound detector as sound type " + std::to_string((size_t)type)
80+
);
81+
return;
82+
}
83+
84+
InferenceSession session(
85+
context, env.console,
86+
{{*detector, std::chrono::milliseconds(20)}}
87+
);
88+
context.wait_until_cancel();
89+
90+
std::cout << "Audio test program Sound listener finished." << std::endl;
91+
}
92+
93+
94+
95+
}
96+
}
97+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Sound Listener
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
* Debug program to test all kinds of sound detectors.
6+
*/
7+
8+
#ifndef PokemonAutomation_PokemonLGPE_SoundListener_H
9+
#define PokemonAutomation_PokemonLGPE_SoundListener_H
10+
11+
#include "Common/Cpp/Options/BooleanCheckBoxOption.h"
12+
#include "Common/Cpp/Options/EnumDropdownOption.h"
13+
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"
14+
//#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h"
15+
16+
namespace PokemonAutomation{
17+
namespace NintendoSwitch{
18+
namespace PokemonLGPE{
19+
20+
21+
class SoundListener_Descriptor : public SingleSwitchProgramDescriptor{
22+
public:
23+
SoundListener_Descriptor();
24+
};
25+
26+
27+
class SoundListener : public SingleSwitchProgramInstance{
28+
public:
29+
SoundListener();
30+
31+
virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override;
32+
33+
virtual void start_program_border_check(
34+
CancellableScope& scope,
35+
VideoStream& stream,
36+
FeedbackType feedback_type
37+
) override{}
38+
39+
private:
40+
enum class SoundType{
41+
SHINY,
42+
};
43+
44+
EnumDropdownOption<SoundType> SOUND_TYPE;
45+
BooleanCheckBoxOption STOP_ON_DETECTED_SOUND;
46+
};
47+
48+
49+
50+
51+
}
52+
}
53+
}
54+
#endif

0 commit comments

Comments
 (0)